我正在使用tsc构建任务。不幸的是,我总是从节点模块文件夹得到相同的错误

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.

我已经在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",
      ]
    }

我哪里做错了?我该怎么做才能忽略这些错误呢?

我使用的是VsCode和tsc 2.9.2版


在"compilerOptions"中添加一个空的"types"选项:

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

从https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

@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


快速补救是跳过检查

{
  "compilerOptions": {
    "skipLibCheck": true
  },
}

我在typescript@3.2.1上遇到了这个问题,并通过升级到3.7.3修复了它。

请注意,对于typescript@3.2.1, skipLibCheck不会生效。当我升级TypeScript时,skipLibCheck: true工作。


您可以在命令行上完成此操作

tsc --skipLibCheck

在tsconfig.json中设置"skipLibCheck": true


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).


我在使用纱线工作区的monorepo中遇到了类似的问题。

结果我的应用程序使用了不同的TypeScript版本的根工作空间,使它们同步解决了这个问题。

你可以在你自己的repo中运行yarn list typescript或npm ls typescript来验证这一点。

如果你有多个版本的TypeScript,那么把你的项目升级到低版本,这样你的repo中就都有当前使用的最高版本,例如yarn add -D typescript@4.5.5(或者任何你需要的版本)


如果你在这里,但下面的方法对你都不起作用:

❌ 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

我想从typescript配置文档中留下这个链接给你: 这(“类型”)会影响什么?

有了这些知识,这为我解决了问题:

查看在运行tsc时是哪个模块导致了日志中的错误。(对我来说是node_modules/mongoose/types/*,所以mongoose是罪魁祸首。) 将您在ES6中导入的所有该模块转换为commonjs的require()。(在我的例子中,import mongoose from 'mongoose'—> const mongoose = require('mongoose'))

我希望这能解决你的问题


升级你的typescript和ts-node: :“打印稿4.9.4” :“ts-node 10.9.1”

并在构建或将其放在tsconfig的compilerOtions中时使用——skipLibCheck标志。json文件("skipLibCheck": true)…