修复 TS2688:在 node_modules 中找不到类型定义文件

问题描述 投票:0回答:4

运行 tsc 时,我得到了很多

TS2688: Cannot find type definition file for 'someLibrary'
这些库来自
node_modules
。我尝试在 tsconfig 中排除
node_modules
skipLibCheck
,但它们都不适合我。知道为什么会发生这种情况吗?

这是我的 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"
  ]
}

typescript tsc tsconfig
4个回答
13
投票

问题是错误的

typeRoots
。应该使用默认值
./node_modules/@types


4
投票

ts.config.json
中,您已默认放置 typeRoots。像这样:

"compilerOptions": {
    ...
    "typeRoots": ["./node_modules/@types"]
}

2
投票

参加聚会有点晚了,但我在项目目录之外安装非全局 npm 模块时遇到了这个问题。我花了太长时间才意识到。


0
投票

对于我的NestJs应用程序

"typeRoots": ["./node_modules/@types"]
没有解决问题

这个问题是因为我有一个包含 DevDependency“@types/x”的包,但我在 package.json 中没有该类型的 DevDependency 的依赖项。

© www.soinside.com 2019 - 2024. All rights reserved.