Rahul Ganguly
Rahul Ganguly

Reputation: 2106

typescript compiling types under node_modules even after excluding them

While trying to compile my typescript source code, I see that the compiler is also trying to compile the types under my node_modules folder . I am using typescript 2.6.1 and my tsconfig file is as below

 {
  "compilerOptions": {
  "allowSyntheticDefaultImports":true,
  "outDir": "./dist",
  "mapRoot": "./dist",
  "module": "commonjs",
  "target": "es6",
  "sourceMap": true,
  "sourceRoot": "./source",
  "removeComments": false
},
"exclude": [
  "node_modules",
  "test"
],
  "include": [
  "source/*.ts"
]
}

When I run the following comand "tsc -w -p tsconfig.json" I get the following error

 node_modules/@types/es6-promise/index.d.ts(11,15): error TS2300: Duplicate identifier 'Promise'.

Upvotes: 9

Views: 17598

Answers (3)

user2684619
user2684619

Reputation: 31

I had to make sure my version of Microsoft.TypeScript.MsBuild matched the version of the 'typescript' devDependency in packages.json file ( in my case 4.6.3 ). After I downgraded the Microsoft.TypeScript.MsBuild assembly to the matching version, I right-clicked the "node_modules" folder it added and hit 'exclude from project'. Finally getting a reasonable amount of errors from TypeScript related to the source code I'm actually working on.

Upvotes: 1

Christopher Oliver
Christopher Oliver

Reputation: 49

Why?

This can also occur when TypeScript versions don't correspond.

E.g. You are running 2.9.x in your global npm cache, and you have TypeScript 3.5.x installed locally in your project node_modules.


Test

You can test this by running "npx tsc", but this will only work if you have TypeScript saved as a dependency, you've run "npm install", and you have npm 5.2.x or greater.

Otherwise you can check your local TypeScript version with "npm list typescript", and your global TypeScript version with "npm list typscript -g"


Solution

If in the case of the "npx tsc" approach, it passes, or, in the second case of the versions being mismatched, you should only need to ensure that you align your local TypeScript version with your global TypeScript version, or vice versa.


Other Notes:

"npx" will run the command using the Node package from your project's local "node_modules" folder.

Check out Node Version Manager if you don't know about it, it's great for switching between Node versions on the fly.

Upvotes: 2

Rahul Ganguly
Rahul Ganguly

Reputation: 2106

After reading this document, I got the answer https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

under section @types, typeRoots and types

They have mentioned

Specify "types": [] to disable automatic inclusion of @types packages.

The updated tsconfig.json is as follows

{
  "compilerOptions": {
   "allowSyntheticDefaultImports":true,
   "outDir": "./dist",
   "mapRoot": "./dist",
   "module": "commonjs",
   "target": "es6",
   "sourceMap": true,
   "sourceRoot": "./source",
   "removeComments": false,
   "types": []
 },
 "exclude": [
   "node_modules",
   "test"
 ],
 "include": [
   "source/*.ts"
 ]
}

Upvotes: 19

Related Questions