Ken
Ken

Reputation: 947

"exclude" property of tsconfig.json is not being respected

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.

Upvotes: 67

Views: 71386

Answers (9)

user13930041
user13930041

Reputation: 191

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"
  ]
...
}

Upvotes: 19

LDJ
LDJ

Reputation: 7304

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!

Upvotes: 2

jmunsch
jmunsch

Reputation: 24089

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:

Upvotes: 64

Michael
Michael

Reputation: 4090

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.

Upvotes: 13

Sam Spade
Sam Spade

Reputation: 1486

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.

Upvotes: 23

NSjonas
NSjonas

Reputation: 12032

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.

Upvotes: 55

emery.noel
emery.noel

Reputation: 1185

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.

Upvotes: -4

user3428449
user3428449

Reputation: 39

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"
]

Upvotes: -2

MartyIX
MartyIX

Reputation: 28648

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.

Upvotes: 1

Related Questions