Recently, I started working with TypeScript and Angular 2, but encountered a problem that has left me puzzled. Initially, everything was going smoothly with the angular2 quickstart project until I attempted to import a plain JavaScript file.
import * as myModule from './mymodule.js';
Despite adding allowJs: true
to the tsconfig.json file to allow JavaScript imports, the compiler threw this error:
error TS5055: Cannot write file '.../mymodule.js' because it would overwrite input file.
From what I understand, the TypeScript compiler should not try to compile a JavaScript file, but for some reason, I can't seem to exclude it from compilation.
I even tried adding the file to the exclude: []
array in tsconfig.json, but since it's imported in one of my .ts files, it doesn't work as expected (according to the TypeScript docs).
So, how can I successfully compile my project with that JavaScript file included? Am I missing a setting or is there an issue with my approach?
Any guidance or advice would be greatly appreciated as I'm getting quite frustrated by this situation.
Additional Information
Here is the tsconfig file I'm using:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
}
}
To build my project, I run:
npm run build
This command invokes a script in the package.json file that looks like this:
"scripts": {
"build": "tsc -p src/",
...
After following the suggestions provided by @Seamus, I still encounter the same error.
However,
If I manually run:
tsc -p src/
I receive a different error message:
error TS7016: Could not find a declaration file for module 'mymodule'
It seems like the module is being recognized when done manually, but why does it fail with the npm run build
? Is there something different happening under the hood?