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

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

我正在使用 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

node.js typescript tsc tsconfig
13个回答
443
投票

快速修复是跳过检查

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

70
投票

在“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/tsconfig#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 包


68
投票

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

tsc --skipLibCheck

45
投票

我在使用

[email protected]
时遇到了这个问题,并通过将其升级到
3.7.3
来修复。

请注意,

[email protected]
skipLibCheck
不生效。当我升级 TypeScript 时,
skipLibCheck: true
可以工作。


42
投票

"skipLibCheck": true
设置在
tsconfig.json


9
投票

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

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

您可以通过运行

yarn list typescript
npm ls typescript
在您自己的存储库中验证这一点。

如果您有多个版本的 TypeScript,请使用较低版本升级您的项目,以便所有项目都具有当前存储库中使用的最高版本,例如

yarn add -D [email protected]
(或您需要的任何版本)


5
投票

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

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

     npm install typescript@latest -g

  • 添加/编辑 tsconfig 选项: 目标、类型、包含、排除、allowJs、skipLibCheck

  • npm 更新

  • 删除node_modules && npm i

  • 删除包锁(不要这样做)&& npm i

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

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

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

我希望这能解决您的问题


4
投票

升级您的打字稿和 ts-node: “打字稿”:“4.9.4” “ts-node”:“10.9.1”

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


4
投票

发生在我身上的是因为我不小心从库中导入了一些不属于公共 API 的内容。 例如。

import { ValidationErrorCode } from '@apollo/server/src/plugin/schemaReporting/generated/operations';
导致
tsc
报告
@apollo/server
中的类型错误。从
src/
dist/
导入应该始终是一个危险信号。


3
投票

如果您发现自己在这里并且其他答案都无法解决问题,请检查以确保您尚未设置

maxNodeModuleJsDepth
。 如果您启用了
allowJs
,则此选项会让 TypeScript 尝试从
node_modules
中的模块推断类型,而
skipLibCheck
对此没有任何影响,因为它正在读取 javascript 文件而不是类型声明。
maxNodeModuleJsDepth
的默认设置是
0
,在绝大多数情况下这就是您想要的(并且您应该更喜欢使用
@types
包而不是打开它)。


3
投票

这是随机的,但这里的解决方案都不起作用。我在节点模块中发生了错误,修复它的是:

改变:

target: "esnext"

对于任何特定版本,例如:

target: "es2020"

万一它可以帮助其他一些贫穷的旅行者🤷u200d♂️


2
投票

为什么不指定类型 root,这样父级“../node_module”、“../../node_modules:”等将被忽略,只采用本地类型。

"typeRoots": ["./node_modules/@types"]

PS 这对我来说不起作用,只有上述方法。

"skipLibCheck": true

0
投票

这里的建议都不适合我。我只需要将 TypeScript 从 v4 升级到 v5

npm install -D typescript@latest
© www.soinside.com 2019 - 2024. All rights reserved.