node_modules 文件夹中的文件出现 [TS] 错误

Getting [TS] errors for files inside the node_modules folder

我只是想使用 webpack5 和 webpack-cli 开始一个新的 typescript 项目。令我惊讶的是,我收到了这些错误:

错误:

hha@melchia ~/projects/project $ npm run build

> my-webpack-project@1.0.0 build /home/hha/projects/project
> webpack

99% done plugins watchInfo[webpack-cli] Compilation finished
asset main.js 4.7 KiB [compared for emit] (name: main)
runtime modules 668 bytes 3 modules
./src/index.ts 616 bytes [built] [code generated]

ERROR in /home/hha/projects/project/node_modules/webpack/lib/javascript/JavascriptParser.js
[tsl] ERROR in /home/hha/projects/project/node_modules/webpack/lib/javascript/JavascriptParser.js(3272,7)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/webpack/lib/ProgressPlugin.js
[tsl] ERROR in /home/hha/projects/project/node_modules/webpack/lib/ProgressPlugin.js(392,4)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/webpack/lib/optimize/ConcatenatedModule.js
[tsl] ERROR in /home/hha/projects/project/node_modules/webpack/lib/optimize/ConcatenatedModule.js(1470,6)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/webpack/lib/util/semver.js
[tsl] ERROR in /home/hha/projects/project/node_modules/webpack/lib/util/semver.js(43,2)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/webpack/lib/util/semver.js
[tsl] ERROR in /home/hha/projects/project/node_modules/webpack/lib/util/semver.js(45,2)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/webpack/lib/util/semver.js
[tsl] ERROR in /home/hha/projects/project/node_modules/webpack/lib/util/semver.js(282,3)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/webpack/lib/util/registerExternalSerializer.js
[tsl] ERROR in /home/hha/projects/project/node_modules/webpack/lib/util/registerExternalSerializer.js(302,4)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/webpack/lib/stats/StatsFactory.js
[tsl] ERROR in /home/hha/projects/project/node_modules/webpack/lib/stats/StatsFactory.js(146,6)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/webpack/lib/stats/StatsFactory.js
[tsl] ERROR in /home/hha/projects/project/node_modules/webpack/lib/stats/StatsFactory.js(193,6)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/webpack/lib/WebpackOptionsApply.js
[tsl] ERROR in /home/hha/projects/project/node_modules/webpack/lib/WebpackOptionsApply.js(520,6)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/jest-worker/build/index.js
[tsl] ERROR in /home/hha/projects/project/node_modules/jest-worker/build/index.js(55,7)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/jest-worker/build/index.js
[tsl] ERROR in /home/hha/projects/project/node_modules/jest-worker/build/index.js(145,7)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/jest-worker/build/index.js
[tsl] ERROR in /home/hha/projects/project/node_modules/jest-worker/build/index.js(171,9)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/webpack/lib/config/normalization.js
[tsl] ERROR in /home/hha/projects/project/node_modules/webpack/lib/config/normalization.js(127,6)
      TS2578: Unused '@ts-expect-error' directive.

ERROR in /home/hha/projects/project/node_modules/webpack/lib/node/nodeConsole.js
[tsl] ERROR in /home/hha/projects/project/node_modules/webpack/lib/node/nodeConsole.js(56,3)
      TS2578: Unused '@ts-expect-error' directive.

Webpack.config.js:

const path = require('path');
const webpack = require('webpack');

module.exports = {
  mode: 'development',
  entry: './src/index.ts',
  plugins: [new webpack.ProgressPlugin()],

  module: {
    rules: [{
      test: /\.(ts|tsx)$/,
      loader: 'ts-loader',
      include: [path.resolve(__dirname, 'src')],
      exclude: [/node_modules/]
    }]
  },

  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  }
}

Tsconfig.json:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "module": "es6",
    "target": "es5",
    "allowJs": true,
    "skipLibCheck": true
  },
  "exclude": [
    "node_modules"
  ]
}

Webpack 信息:

  System:
    OS: Linux 5.4 Ubuntu 20.04.1 LTS (Focal Fossa)
  Binaries:
    Node: 12.18.1 - ~/.nvm/versions/node/v12.18.1/bin/node
    Yarn: 1.22.4 - ~/.nvm/versions/node/v12.18.1/bin/yarn
    npm: 6.14.8 - ~/.nvm/versions/node/v12.18.1/bin/npm
  Browsers:
    Chrome: 86.0.4240.111
    Firefox: 82.0
  Packages:
    terser-webpack-plugin: ^5.0.3 => 5.0.3 
    webpack: ^5.3.2 => 5.3.2 
    webpack-cli: ^4.1.0 => 4.1.0 
  Global Packages:
    webpack-cli: 4.1.0
    webpack: 5.3.2

所以我不得不更改 tsconfig 选项 include 以指定 typescript 将分析的目录。

根据documentation on include option,默认值为:

[] if files is specified, otherwise ["**/*"]

所以默认行为是分析所有存储库,包括 node_modules

解决方案:

总结一下,只需编辑 include 属性以仅包含 src 文件夹:

{
  "include": ["src/**/*"]
}

Another Whosebug answer 帮助了我:

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

在我的例子中,excludeskipLibCheck 都设置了,但它仍然试图编译 node_modules

中的文件

最后,我发现了一些模块的深度导入,将其源代码打包到可分发文件中,如下所示:import {Something} from "some-module/src/something.ts"

删除深度导入解决了问题,import {Something} from "some-module"