52

I am working with the excellent Express/Node/Typescript example code found here. It transpiles the .ts code with the following command from run.sh:

./node_modules/.bin/tsc --sourcemap --module commonjs ./bin/www.ts

This works as advertised, but I would prefer to use a tsconfig.json file and tsc -p . However, when I run that command I get a raft of TS2300: Duplicate identifier 'foo' errorswhen tsc(erroneously?) tries to walk the ./node_modules and ./typings directories. Below is the tsconfig.json I am using:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings"
  ]
}

Any ideas? I am using tsc 1.7.3 FWIW.

Ken
  • 797
  • 1
  • 5
  • 11
  • Are you sure you're using the right tsc version, I had problems with multiple installed versions on my own. If you are not sure use tsc -v – Ced Dec 16 '15 at 12:59
  • The Typescript compiler will still walk excluded directories if you have explicitly imported a file that (whether directly, or in a sub-import of its own) ends up importing from that excluded directory. Same answer as in this [duplicate question](https://stackoverflow.com/questions/37210246/tsc-seemingly-not-picking-up-exclude-options-fro-tsconfig-json). – Jamie Birch Mar 23 '19 at 19:34
  • With respect to Imports that "Jamie B" mentioned above. These imports can dramatically change your "OutDir" structure. e.g I had a env variables file in a root folder of a multi project structure. The sub Project referenced this env file, and when doing tsc --build on the subproject, it then created a relative output structure in the Dist that included the parent folder of the env. Thus destroying the intent to encapsulate the dist for each subproject. – redevill Aug 18 '20 at 00:11

9 Answers9

59

In a similar vein I was having issues with node_modules exclusion.

Here's a heavy handed solution, that ignores all *.d.ts files.

I added to compilerOptions:

"compilerOptions": {
    "skipLibCheck": true,
    ...
 }

Related:

jmunsch
  • 22,771
  • 11
  • 93
  • 114
29

Typescript will pull in any path that is referenced by an import statement in the files that are part of the project.

If you are seeing files getting processed that you have "excluded", then check for references to them in other code.

NSjonas
  • 10,693
  • 9
  • 66
  • 92
  • 3
    This worked for me, I had been importing some files from node_modules directly and couldn't figure out why they weren't being excluding. Thanks! – jargetz Jun 29 '21 at 15:16
13

I found that adding two asterisks and a slash (**/) prior to the directories to exclude solved the problem, edit the tsconfig file as follows:

{
...
  "exclude": [
    "**/node_modules",
    "**/typings"
  ]
...
}
user13930041
  • 131
  • 1
  • 2
  • Why is it that the `**` made the difference here? – Dom Hallan Dec 04 '20 at 07:41
  • 2
    The paths in the `tsconfig.json` are relative to the file itself. E.g. if your `node_modules` is in a subdirectory, you need to specify the exact path (`path/to/node_modules`), or put the `**`, which means in an arbitrary subdirectory. – panepeter Mar 11 '21 at 06:28
12

Okay, if you've tried everything else, check that there are no import statements that lead to "excluded" code in your codebase. If you include a file in the Typescript scope that imports a file from (for example) "compiled-src", then all files that imports will suddenly become included. This can have a domino effect that makes it appear that the exclude property is not being respected.

Specifically, I used intellisense to auto-import something from within my codebase, and intellisense decided that it prefered the file in compiled-src to the typescript file. I didn't notice and it took ages to realize what had happened.

Sam Spade
  • 1,107
  • 7
  • 20
  • 1
    This is the only helpful answer in this thread; everyone else is recommending bad, project specific configurations like ignoring d.ts files or setting different ES targets. When in reality, this is probably the underlying issue. – Nathanael Jul 23 '22 at 20:48
9

I do see that this was some time ago, and also that you've already accepted an answer, but I would like to add the following, which is documented here:

"Important: exclude only changes which files are included as a result of the include setting."

Since there is no include setting in your tsconfig, that exclude setting would have no effect.

Michael
  • 4,010
  • 4
  • 28
  • 49
3

I just had the same problem. Pulling my hair out for about 30 mins then discovered that if I change:

"target": "ES5",

to

"target": "ES6",

All the errors go away!

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
LDJ
  • 6,896
  • 9
  • 52
  • 87
  • I added typings for core-js and kept target at "es5". – CoderDennis Jun 08 '16 at 17:04
  • As CoderDennis mentioned above, it's because if you are targetting ES5, you need to add types for ES6 to your config. See this solution for details -> https://stackoverflow.com/a/38959037/2075602 – U4EA Sep 29 '20 at 12:00
1

I did:

git clone https://github.com/czechboy0/Express-4x-Typescript-Sample.git
cd Express-4x-Typescript-Sample/
./run.sh
tsd install  # I don't know why, but this helped me.
./run.sh

I created Express-4x-Typescript-Sample/tsconfig.json file with content

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings"
  ]
} 

I ran

[...]/Express-4x-Typescript-Sample$ tsc -p .

and it works work me - i.e. there are no errors.

MartyIX
  • 27,828
  • 29
  • 136
  • 207
  • `tsd` is/was used for TypeScript definitions, that is for those third-party libs written in JS that miss their *declaration* in TS. `tsd install` should make those definitions available, so that TS compiler can successfully build. It reminds me of C/C++ *include.h* for third-party libs. – superjos Feb 22 '16 at 15:31
-1

I had this issue and my exclude looked like this:

"exclude": [
  "node_modules",
  "typings"
]

When I removed the "typings" it worked. Final solution for me:

"exclude": [
  "node_modules"
]
-4

04/02/0217 (april 2) - I was going through this same thing, spent almost a full weekend on it. Finally I found this website (which I have never seen linked from any stackoverflow post): https://angular.io/docs/ts/latest/guide/typescript-configuration.html

In it, I found this line, right in the compilerOptions:

"lib": [ "es2015", "dom" ]

I have no idea what it does, and at this point I don't care, but ALL of my node_modules errors went away.

As for include/exclude not working, I believe it is because of "dependencies." Even if you exclude a folder, if an imported file (like Component or NgModule) has some dependency on a file in node_modules, tsc is going to try to compile that file.

emery.noel
  • 1,073
  • 1
  • 9
  • 24