How to force tsc to ignore node_modules folder?
Asked Answered
H

15

303

I'm using tsc build tasks. Unfortunately I'm always getting the same errors from the node modules folder

Executing task: .\node_modules\.bin\tsc.cmd --watch -p .\tsconfig.json <
node_modules/@types/node/index.d.ts(6208,55): error TS2304: Cannot find name 'Map'.
node_modules/@types/node/index.d.ts(6215,55): error TS2304: Cannot find name 'Set'.
node_modules/@types/node/index.d.ts(6219,64): error TS2304: Cannot find name 'Symbol'.
node_modules/@types/node/index.d.ts(6225,59): error TS2304: Cannot find name 'WeakMap'.
node_modules/@types/node/index.d.ts(6226,59): error TS2304: Cannot find name 'WeakSet'.
10:13:18 - Compilation complete. Watching for file changes.

I already added the directory to the ignore at tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
    "strict": false,
    "noImplicitAny": false,
    "strictPropertyInitialization": false,
    "esModuleInterop": true,
  },
  "include": [
    "src/*"
  ],
  "exclude": [
    "node_modules",
    "./node_modules",
    "./node_modules/*",
    "./node_modules/@types/node/index.d.ts",
  ]
}

What I'm doing wrong? What should I do in order to ignore those errors?

I'm using VSCode and tsc Version 2.9.2.

Hexylresorcinol answered 1/8, 2018 at 13:18 Comment(3)
This could be helpful: "...To do so, the compiler needs the definition of a module, this could be a .ts file for your own code, or a .d.ts for an imported definition file. If the file was found, it will be included regardless of whether it was excluded in the previous steps or not." -> github.com/Microsoft/TypeScript/wiki/…Pestilence
I want types. I don't want to have to build my project. To do that, I'm using JSDoc, since it uses comments that runners will ignore. The only CLI I could find that could lint my types against the JSDoc is the Typescript CLI with "noEmit": true and "checkJs": true. But then I'm getting errrors from node_modules/utils/utils.js. I don't want theese errors. How can I remove them?Allantois
Restarting the IDE or already F1 → TypeScript: Reload Project in VSCode helped to make some Reported Problems disappearStalky
W
480

Quickfix is to skip the check

{
  "compilerOptions": {
    "skipLibCheck": true
  },
}
Written answered 26/8, 2019 at 7:28 Comment(14)
This worked since the library I was importing was exporting just the types of its own and I was importing the actual files by using script imports so that i can serve them from cdn. Thanks :)Turcotte
Docs for this: typescriptlang.org/docs/handbook/compiler-options.htmlDanyelledanyette
Works for me with v3.9.7Irreverent
This will also skip your type checking of all declaration *.d.ts dd.engineering/blog/…Baalman
does skipLibCheck only target the node_modules folder ?Chickamauga
@DilipAgheda no it will also target any d.ts files outside of node_modules. So it is probably not what most people want.Deannadeanne
Note that tsc --listFiles seems to ignore this argument, it will still display node_modules typings (TS 4.2).Reimer
@GianfrancoP.Apparently it will not completely disable .d.ts files. Any types your code reaches directly will still be included from those files. Only .d.ts files as a whole won't be checked any more. -- dd.engineering/blog/…Chilt
Didn't work for version 3.3.x but worked after i upgraded to v4.5Grosbeak
This doesn't work if you have checkJs enabled and a node module contains js files.Trolly
This absolutely does NOT work. I've been banging my head against this for weeks. If you include a module, it gets checked, and there appears to be no way to stop this from happening.Ronda
Explanation of what skitLibCheck does: typescriptlang.org/tsconfig#skipLibCheckLeilaleilah
Better answer that only ignores node_modules: set types=[].Umont
this has no effect.Anesthesiologist
C
76

Add an empty "types" option in "compilerOptions":

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
    "strict": false,
    "noImplicitAny": false,
    "strictPropertyInitialization": false,
    "esModuleInterop": true,
    "types": []
  },
  "include": [
    "src/*"
  ],
  "exclude": [
    "node_modules",
    "./node_modules",
    "./node_modules/*",
    "./node_modules/@types/node/index.d.ts",
  ]
}

From https://www.typescriptlang.org/tsconfig#typeRoots

@types, typeRoots and types

By default all visible “@types” packages are included in your compilation. Packages in node_modules/@types of any enclosing folder are considered visible; specifically, that means packages within ./node_modules/@types/, ../node_modules/@types/, ../../node_modules/@types/, and so on.

...

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

Keep in mind that automatic inclusion is only important if you’re using files with global declarations (as opposed to files declared as modules). If you use an import "foo" statement, for instance, TypeScript may still look through node_modules & node_modules/@types folders to find the foo package

Coypu answered 20/1, 2019 at 15:6 Comment(5)
@MichaelOzeryansky: sorry, I don't understand your point. I just added one line to the tsconfig.json of the OPCoypu
@MichaelOzeryansky this addition would probably make your compile run faster. More lines does not automatically make compile time slower. Less lines does not make your code run faster. The caveat here is that this would prevent many projects from compiling without other significant changes due to no longer including types they need.Chronogram
Specifying an empty list for CLI worked for me like this tsc ./myfile.ts --types --someOtherOptionWeidman
This generated even more errorsAnesthesiologist
This deleted answer (10k+ rep to view) says "I would advise against setting "types": [] - you are excluding all other global types in node_modules/@types from being included automatically". It'd be good if this answer listed any caveats or downsides to this approach. It does work for me, but I'm a bit hesitant to use if it excludes types outside of node_modules that I want to check.Tuddor
M
70

You can do this right on the command line

tsc --skipLibCheck
Magically answered 11/3, 2021 at 8:34 Comment(3)
this is better because at least the library types are validated during the webpack build if you're using webpack plugins to validate the included library typesOverspill
This worked for me (TS 4.4.4). Even though I had also had it specified in the tsconfig, apparently I explicitly needed to do it like your solution..Viewing
EDIT: Regarding my comment above; I didn't RTFM, I specified a file in my tsc command, which, as is clearly stated in the docs, will ignore the tsconfig; typescriptlang.org/docs/handbook/…Viewing
B
47

I met this issue with [email protected] and fixed by upgrading it to 3.7.3.

Notice with [email protected], the skipLibCheck does not take effect. When I upgraded TypeScript the skipLibCheck: true works.

Burglarize answered 5/12, 2019 at 12:38 Comment(1)
was helpful, upgraded to latest. 3.9.7 and added skipLibCheckEshelman
W
10

I had a similar issue in a monorepo using yarn workspaces.

Turned out my app was using a different TypeScript version to the root workspace and bringing those in sync fixed the issue.

You can verify this in your own repo by running yarn list typescript or npm ls typescript.

If you have multiple versions of TypeScript then upgrade your projects with the lower version so that all have the highest version currently in use in your repo, e.g. yarn add -D [email protected] (or whatever version you need)

Whitney answered 26/5, 2022 at 13:38 Comment(2)
This is the only solution that works for me, I had different version on my global and my localUneven
Same issue but with npm workspaces. Thank you!Club
D
7

if you are here and none of the following has worked for you:

  • Updgrade/downgrading typescript version (NOTE: When you run the tsc command, the globally installed typescript version is used as the compiler. So even if you have the latest typescript version in your package.json, you will have to upgrade typescript globally. With npm thats npm install typescript@latest -g )

  • Adding/Editing tsconfig options: target, types, include, exclude, allowJs, skipLibCheck

  • npm update

  • Deleting node_modules && npm i

  • Deleting package-lock (don't do that btw) && npm i

I want to leave this link from the typescript config docs here for you: What does this("types") affect?

With this knowledge, this solved the issue for me:

  1. See what module is causing the error in the log when you run tsc. (For me it was node_modules/mongoose/types/*, so mongoose was the culprit.)
  2. Convert all your ES6 imports of this module to commonjs's require(). (In my case import mongoose from 'mongoose' --> const mongoose = require('mongoose') )

I hope this solves your issue

Debbradebby answered 12/10, 2022 at 8:53 Comment(0)
D
6

Happened for me b/c I accidentally imported something from a library with was not part of the public API. E.g. the import { ValidationErrorCode } from '@apollo/server/src/plugin/schemaReporting/generated/operations'; caused tsc to report type errors in @apollo/server. Importing from src/ or dist/ should always be a red flag.

Downcome answered 14/7, 2023 at 10:47 Comment(1)
This was my issue, I was importing types not otherwise output by react-map-gl and forgot about it, causing this issue. Thanks for being spot on for me, was pulling my hair on this one.Crunch
L
5

Upgrade your typescript & ts-node: "typescript": "4.9.4" "ts-node": "10.9.1"

and use the flag --skipLibCheck when you build or put it in the compilerOtions on the tsconfig.json file ("skipLibCheck": true)...

Legible answered 19/1, 2023 at 22:42 Comment(0)
O
5

Why not specify the types root, this way the parent "../node_module", "../../node_modules:" etc. will be ignored and only local types will be taken.

"typeRoots": ["./node_modules/@types"]

PS This was not working for me in my case only the above approach.

"skipLibCheck": true
Ophicleide answered 3/8, 2023 at 18:27 Comment(0)
H
4

If you find yourself here and none of the other answers is solving the problem, check to make sure that you haven't set maxNodeModuleJsDepth. If you have allowJs enabled this option lets TypeScript attempt to infer types from modules in node_modules, and skipLibCheck doesn't have any effect on that, because it's reading javascript files and not type declarations. The default setting for maxNodeModuleJsDepth is 0, and in the vast majority of cases that's what you want (and you should prefer using @types packages instead of turning this on).

Hippocampus answered 7/5, 2022 at 20:37 Comment(0)
S
3

This is random but none of the solutions here worked. I had an error occuring in a node_module and what fixed it was this:

Changing: target: "esnext"

To any specific version, e.g.: target: "es2020"

In case it helps some other poor traveller 🤷‍♂️

Sarracenia answered 31/5, 2022 at 17:45 Comment(0)
G
1

None of the suggestions here worked for me. I only had to upgrade TypeScript from v4 to v5

npm install -D typescript@latest
Gershwin answered 14/11, 2023 at 12:28 Comment(0)
P
0

here's how you would do so in a bun project!

{
  "compilerOptions": {
    "types": ["bun-types"],
    "module": "esnext",
    "target": "esnext",
    "lib": ["ESNext"],
    "declaration": false,
    "strict": true,
    "moduleResolution": "node",
    "skipLibCheck": true,
    "outDir": "lib"
  },
  "include": ["src"],
  "exclude": ["node_modules", "reminder"]
}
Phallic answered 23/11, 2023 at 5:52 Comment(0)
A
0

If your 3rd party library is giving you .ts files directly (no d.ts. and not .js), the best way to do it is to use a script to add // @ts-nocheck to each file in node_modules.

I use the following for a CI environment setup.

(subsitute npm equivalent for these yarn commands)

% yarn 
% find node_modules -type f -name '*.ts' -exec sh -c 'echo "// @ts-nocheck" > /tmp/file.tmp && cat "$1" >> /tmp/file.tmp && mv /tmp/file.tmp "$1"' _ {} \;
% yarn tsc

This here is the magic: find node_modules/@rally -type f -name '*.ts' -exec sh -c 'echo "// @ts-nocheck" > /tmp/file.tmp && cat "$1" >> /tmp/file.tmp && mv /tmp/file.tmp "$1"' _ {} \;

This will go into the node_modules and find each typescript file (file that ends in .ts) and insert a // @ts-nocheck to the top of each file, recursively.

After running the above command, tsc will still run for everything outside node_modules, but will not check the node_modules files.

Atlantis answered 18/1 at 23:32 Comment(0)
V
0

If it helps anyone, I fixed my issue by resolving the differences in the typescript version on my computer and the typescript version in my project.

A. Get TS version of your comp 'tsc -v' (e.g. Version 5.4.5)

B. In package.json for your project > "devDependencies": { "typescript": "^3.9.10"}, change to match your comp's tsc verion: "devDependencies": { "typescript": "^5.4.5"}

C. run 'npm i' which will install the new version for your project.

Try running 'npm build' and it should work.

Alternatively, you can change the tsc version of your computer to match the project.

Virus answered 17/4 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.