14

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"
  ]
}

Brian Law
  • 460
  • 1
  • 4
  • 18

4 Answers4

12

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

Brian Law
  • 460
  • 1
  • 4
  • 18
  • 2
    Please can you explain this answer? Your original file contains `./node_modules/@types` in the array `typeRoots` – Stewart May 16 '22 at 19:33
  • 3
    I think he means "to add" "typeRoots": [ "./node_modules/@types" ], – Danil Jul 31 '22 at 10:10
  • 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. – Frank N Aug 08 '22 at 19:19
3

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

"compilerOptions": {
    ...
    "typeRoots": ["./node_modules/@types"]
}
starball
  • 20,030
  • 7
  • 43
  • 238
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.

-5

My ts.config.json didn't have typeRoots , I just put it there, and it worked fine

{
  "compilerOptions": {
    ...
    "typeRoots": ["./node_modules/@types"]
    ...
  }
}
Rizwan
  • 363
  • 3
  • 10
  • 1
    Yes, [this was said 2 years ago](https://stackoverflow.com/a/63626170/542251). Please don't just repeat answers – Liam Apr 12 '23 at 16:13