Melchia
Melchia

Reputation: 24324

Getting [TS] errors for files inside the node_modules folder

I just wanted to start a new typescript project using webpack5 and webpack-cli. In my surprise I'm getting these errors:

Errors:

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

> [email protected] 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 info:

  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

Upvotes: 10

Views: 18570

Answers (4)

Pavel
Pavel

Reputation: 2564

In my case import of some @storybook v8 produces 400+ errors in declaration:true mode. Removed storybook-related files from compilation - errors gone.

Upvotes: 0

n1kk
n1kk

Reputation: 1246

In my case, both exclude and skipLibCheck were set, but it still tried to compile files in node_modules

In the end, I found a deep import of some module that had its sources packaged into the distributable, like so:

import {Something} from "some-module/src/something.ts"

Removing that deep import resolved the issue,

import {Something} from "some-module"

Upvotes: 36

StackOverflowUser
StackOverflowUser

Reputation: 1133

Another StackOverflow answer helped me:

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

Upvotes: 10

Melchia
Melchia

Reputation: 24324

So it turned out I had to change tsconfig option include to specify the directory which typescript will analyse.

According to documentation on include option the default value is:

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

So the default behavior is to analyse all repository including node_modules.

Solution:

To sum up just edit include attribute to include only the src folder:

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

Upvotes: 7

Related Questions