首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何强制tsc忽略node_modules文件夹?

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

Stack Overflow用户
提问于 2018-08-01 21:18:29
回答 6查看 80.1K关注 0票数 150

我正在使用tsc构建任务。不幸的是,我总是从node modules文件夹中得到相同的错误

代码语言:javascript
复制
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的ignore中

代码语言:javascript
复制
    {
      "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

EN

回答 6

Stack Overflow用户

发布于 2019-08-26 15:28:34

快速修复是跳过检查

代码语言:javascript
复制
{
  "compilerOptions": {
    "skipLibCheck": true
  },
}
票数 243
EN

Stack Overflow用户

发布于 2019-01-20 23:06:55

在“compilerOptions”中添加空的"types“选项:

代码语言:javascript
复制
{
  "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和类型

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

..。

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

请记住,只有在使用带有全局声明的文件(而不是声明为模块的文件)时,自动包含才是重要的。例如,如果使用导入" foo“语句,TypeScript可能仍然会在node_modules & node_modules/@types文件夹中查找foo包

票数 34
EN

Stack Overflow用户

发布于 2019-12-05 20:38:15

我在typescript@3.2.1中遇到了这个问题,并通过将其升级到3.7.3进行了修复。

注意,使用typescritp@3.2.1时,skipLibCheck不会生效。通过升级typescriptskipLibCheck: true可以正常工作。

票数 31
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51634361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档