Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'.
like image 564
Rahul Ganguly Avatar asked Nov 15 '17 09:11

Rahul Ganguly


2 Answers

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"
 ]
}
like image 138
Rahul Ganguly Avatar answered Nov 04 '22 00:11

Rahul Ganguly


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.

like image 34
Christopher Oliver Avatar answered Nov 04 '22 01:11

Christopher Oliver