Fixing TS2688: Cannot find type definition file in node_modules
Asked Answered
E

7

20

When running tsc, I am getting many TS2688: Cannot find type definition file for 'someLibrary' These libraries came from node_modules. I have tried to exclude node_modules and skipLibCheck in tsconfig, but none of them works for me. Any idea why is this happening?

Here's my tsconfig.json

{
  "ts-node": {
    "files": true,
    "emit": true,
    "compilerHost": true
  },
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "target": "es2016",
    "sourceMap": true,
    "typeRoots" : ["./node_modules","./node_modules/@types"],
    "jsx": "react",
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonJS",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
  },
  "exclude": [
    "node_modules"
  ]
}

Elnoraelnore answered 27/8, 2020 at 10:3 Comment(1)
if you use vscode, you can press ctrl+shift+p and select Restart TS Server.Burgener
E
15

The problem was the wrong typeRoots. Should use the default ./node_modules/@types

Elnoraelnore answered 28/8, 2020 at 1:21 Comment(4)
Please can you explain this answer? Your original file contains ./node_modules/@types in the array typeRootsIcj
I think he means "to add" "typeRoots": [ "./node_modules/@types" ],Lovejoy
I had "typeRoots" in package.json set to different other folders (for good but unrelated reasons) and it was forgotten to mention the otherwise“default” "node_modules/@types". adding it (as first in array) and restarting vscode fixed the warning.Ryter
So this does seem to work for me as well! One thing to make note of is that "default" to people coming from 4.x.x is not default it seems for 5.x.x and so logic was slightly off.Wrennie
C
9

In ts.config.json you have put the typeRoots by default. Like this:

"compilerOptions": {
    ...
    "typeRoots": ["./node_modules/@types"]
}
Cunaxa answered 18/12, 2022 at 16:9 Comment(1)
This fixes the issue. However, if you have several ts configs you need to update all of themArliearliene
M
2

Bit late to the party, but I had this issue when installing non-global npm modules outside the project directory. Took me far too long to realise.

Marianelamariani answered 5/10, 2022 at 11:4 Comment(0)
G
2

For my NestJs Application "typeRoots": ["./node_modules/@types"] did not solve the issue

The issue as I have a package with DevDependency "@types/x", but I did not have a dependency in package.json for that typed DevDependency.

Gensler answered 30/9, 2023 at 10:26 Comment(0)
S
1

I had a similar issue, where this answer was useful

npm install @types/node --save-dev

(taken from https://mcmap.net/q/204486/-build-cannot-find-type-definition-file-for-39-node-39)

Synaeresis answered 27/5 at 10:46 Comment(0)
O
0

For me, it was a bit of an edge case:

I installed a new package to the project, than removed it by using git reset. This left a folder in node_modules/@types which was empty after the reset.

Deleting the empty folder node_modules/@types/someLibrary solved it.

Osithe answered 9/4 at 14:12 Comment(0)
K
0

Just restarting the typescript server worked for me, ctrl + shift + p search typescript: restart ts server

Kutaisi answered 23/7 at 8:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.