简体   繁体   中英

"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' errors when 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.

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,
    ...
 }

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.

I found that adding two asterisks and a slash (**/) prior to the directories to exclude solved the problem:

{
...
  "exclude": [
    "**/node_modules",
    "**/typings"
  ]
...
}```

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.

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!

Anyone know why?!?

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 - ie there are no errors.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM