Lee Winder Lee Winder - 1 year ago 112
Node.js Question

TypeScript - Importing external npm module causes tsc's output folder to change

I have an TypeScript project set up to build the resultant JavaScript files to ./src/. This works perfectly fine when building against existing npm modules (it's an Angular 2 project so imports those, but I don't think that matters in this case).

When I install a specific npm module (ng2-dynamic-dialog of which I am the author) and import anything from that module, tsc suddently builds the JavaScript to a different folder (rather than build it to ./src/, it's now building to ./src/src/) and it also makes a copy of the node_modules folder in ./src/ which contains just ng2-dynamic-dialog.

This is probably not a great explination so there is an example here
https://github.com/leewinder/ng2-dynamic-dialog (Sha - 8ef439d6)

If you clone the develop branch, and run tsc in ./samples you'll see that the JS files are correctly built to ./src. If you open src/samples/samples.component.ts and uncomment the line 'import { Ng2DynamicDialogComponent } from 'ng2-dynamic-dialog';' and run tsc again, you'll see that src/ now contains ./src/src and ./src/node_modules

My tsconfig.json is set up as follows

"compilerOptions": {
"target": "es5",
"outDir": "src/",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"watch": true,
"diagnostics": true,
"listFiles": true

If I change 'outDir' to ./, then this duplicate folder issue no longer happens, but why would referencing this single npm package cause this issue?

This issue happens whether I have added my NPM module to systemjs.config.js or not, so it doesn't seem related to that.

I can only assume there is something up with either my config or my npm package, but the npm package seems fine.

The source to the npm package is in the same repo as above but in the development folder.

Any help would be seriously appreciated as I'm hitting my head against a brick wall at the moment.

Answer Source

Importing external npm module causes tsc's output folder to change

Use the compilerOptions.baseDir option to lock in the base dir instead of it being magically determined.