typescript compiling types under node_modules even after excluding them
Asked Answered
S

3

9

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'.
Sardius answered 15/11, 2017 at 9:7 Comment(0)
S
20

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"
 ]
}
Sardius answered 15/11, 2017 at 9:21 Comment(1)
OMG this is a godsend answer. I have been struggling with this on a Firebase Functions deploy for an ionic capacitor react project where the typescript compiler was failing as it was picking up node_modules in the root project. Adding "types": [] sorted the issue. Thank you sooooo much.Tullius
D
3

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.

Depersonalization answered 27/8, 2019 at 11:22 Comment(0)
R
1

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.

Ramose answered 22/7, 2022 at 1:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.