"exclude" property of tsconfig.json is not being respected
Asked Answered
P

9

62

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.

Pachston answered 16/12, 2015 at 12:36 Comment(3)
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 -vArsenault
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.Siding
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.Schaeffer
D
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.

Downhearted answered 16/12, 2015 at 12:56 Comment(1)
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.Vernitavernoleninsk
W
65

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:

Wallet answered 4/9, 2019 at 15:49 Comment(5)
I've looked all over and this is what finally worked for me.Fuscous
Despite the "lib" in the name, skipLibCheck is documented to do something different: Skip type checking of all declaration files (*.d.ts)..Phyllous
This is the sauce. Gets me every time I start a new TypeScript project and include RxJs.Amphiarthrosis
Doesn't work for me. skipLibCheck or skipDefaultLibCheckRoveover
it's 2022 now, but this one still worked for meOrgandy
I
46

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.

Interval answered 17/11, 2020 at 23:58 Comment(2)
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!Shakti
This needs more upvotes! This was exactly what my issue was. An included file was importing a file from excluded directory and thus was including it, basically ignoring the exclude. Thank you!Literacy
P
20

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.

Punjab answered 12/5, 2021 at 23:4 Comment(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.Spectrogram
C
18

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"
  ]
...
}
Cajolery answered 18/10, 2020 at 17:59 Comment(2)
Why is it that the ** made the difference here?Diploid
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.Juggler
M
10

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.

Musician answered 14/11, 2021 at 2:50 Comment(0)
S
2

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!

Spatiotemporal answered 17/5, 2016 at 19:30 Comment(2)
I added typings for core-js and kept target at "es5".Petterson
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://mcmap.net/q/323439/-typescript-build-getting-errors-from-node_modules-folderMotmot
D
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.

Downhearted answered 16/12, 2015 at 12:56 Comment(1)
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.Vernitavernoleninsk
F
-2

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"
]
Flycatcher answered 23/7, 2016 at 20:51 Comment(0)
S
-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.

Sodom answered 2/4, 2017 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.