如何强制tsc忽略node_modules文件夹?

Dan*_*tos 7 node.js tsc tsconfig

我正在使用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.
Run Code Online (Sandbox Code Playgroud)

我已经将目录添加到tsconfig.json中的ignore

{
  "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",
  ]
}
Run Code Online (Sandbox Code Playgroud)

我做错了什么?我该怎么做才能忽略这些错误?

我正在使用VsCode和tsc版本2.9.2

Mic*_*Mic 29

在“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",
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

@types、typeRoots 和类型

默认情况下,所有可见的“@types”包都包含在您的编译中。任何封闭文件夹的 node_modules/@types 中的包都被认为是可见的;具体来说,这意味着 ./node_modules/@types/、../node_modules/@types/、../../node_modules/@types/ 等中的包。

...

指定 "types": [] 以禁用自动包含 @types 包。

请记住,仅当您使用带有全局声明的文件(而不是声明为模块的文件)时,自动包含才重要。例如,如果您使用 import "foo" 语句,TypeScript 可能仍会通过 node_modules 和 node_modules/@types 文件夹查找 foo 包

  • @MichaelOzeryansky:抱歉,我不明白你的意思。我刚刚在 OP 的 tsconfig.json 中添加了一行 (5认同)
  • @MichaelOzeryansky 这个添加可能会让你的编译运行得更快。更多行不会自动使编译时间变慢。更少的行数并不会让你的代码运行得更快。这里需要注意的是,这将阻止许多项目在没有其他重大更改的情况下进行编译,因为不再包含它们需要的类型。 (2认同)

Jef*_*ian 23

我遇到了这个问题typescript@3.2.1并通过将其升级到3.7.3.

注意typescritp@3.2.1skipLibCheck不生效。通过升级typescriptskipLibCheck: true作品。

  • 很有帮助,升级到最新的。3.9.7 并添加了skipLibCheck (4认同)
  • 谢谢,我差点砸屏了 (4认同)

Kla*_*urn 12

您可以直接在命令行上执行此操作

tsc --skipLibCheck
Run Code Online (Sandbox Code Playgroud)

  • 编辑:关于我上面的评论;我没有 RTFM,我在“tsc”命令中指定了一个文件,正如文档中明确指出的那样,该文件将忽略 tsconfig;https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#using-tsconfigjson-or-jsconfigjson (2认同)

HNi*_*pps 9

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

事实证明,我的应用程序在根工作区中使用了不同的 TypeScript 版本,并使这些版本同步解决了问题。

您可以通过运行yarn list typescript或在您自己的存储库中验证这一点npm ls typescript

如果您有多个版本的 TypeScript,请使用较低版本升级您的项目,以便所有项目都具有您的存储库中当前使用的最高版本,例如yarn add -D typescript@4.5.5(或您需要的任何版本)

  • 是的。似乎没有其他的方法对我有用,但这个却有用。谢谢! (2认同)
  • 这是唯一适合我的解决方案,我的全局和本地版本有不同的版本 (2认同)

Dan*_*ers 8

快速修复是跳过检查

{
  "compilerOptions": {
    "skipLibCheck": true
  },
}
Run Code Online (Sandbox Code Playgroud)

  • @DilipAgheda 不,它还会针对“node_modules”之外的任何“d.ts”文件。所以这可能不是大多数人想要的。 (8认同)
  • 相关文档:https://www.typescriptlang.org/docs/handbook/compiler-options.html (7认同)
  • 这也将跳过所有声明 `*.d.ts` 的类型检查 https://dd.engineering/blog/typescript-the-skiplibcheck-option-explained (5认同)
  • 这是有效的,因为我导入的库仅导出其自己的类型,并且我通过使用脚本导入来导入实际文件,以便我可以从 cdn 提供它们。谢谢 :) (2认同)
  • skipLibCheck 是否仅针对 node_modules 文件夹? (2认同)
  • 请注意,`tsc --listFiles` 似乎忽略了这个参数,它仍然会显示 node_modules 类型(TS 4.2)。 (2认同)
  • [更好的答案](/sf/answers/3799443651/)仅忽略`node_modules`:设置`types=[]`。 (2认同)

Mik*_*ren 7

如果您在这里并且以下方法都不适合您:

\n
    \n
  • \xe2\x9d\x8c升级/降级 typescript 版本\n(注意:当您运行 tsc 命令时,全局安装的 typescript 版本将用作编译器。因此,即使您的 package.json 中有最新的 typescript 版本,您必须全局升级 typescript。使用 npm 就是这样npm install typescript@latest -g

    \n
  • \n
  • \xe2\x9d\x8c添加/编辑 tsconfig 选项: target、types、include、exclusion、allowJs、skipLibCheck

    \n
  • \n
  • \xe2\x9d\x8c npm 更新

    \n
  • \n
  • \xe2\x9d\x8c删除node_modules && npm i

    \n
  • \n
  • \xe2\x9d\x8c删除包锁(顺便说一句,不要这样做)&& npm i

    \n
  • \n
\n

我想为您留下打字稿配置文档中的链接:\n这(“类型”)会影响什么?

\n

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

\n
    \n
  1. 运行时查看日志中导致错误的模块tsc。(对我来说是的node_modules/mongoose/types/*,所以猫鼬是罪魁祸首。)
  2. \n
  3. 将此模块的所有 ES6 导入转换为 commonjs 的 require()。(就我而言import mongoose from \'mongoose\'--> const mongoose = require(\'mongoose\')
  4. \n
\n

我希望这能解决您的问题

\n


Pen*_*gin 5

设置"skipLibCheck": true在里面tsconfig.json


Meh*_*che 5

升级您的打字稿和 ts-node: "typescript": "4.9.4" "ts-node": "10.9.1"

并在构建或将其放入 tsconfig.json 文件上的 compilerOtions 时使用标志 --skipLibCheck ("skipLibCheck": true)...