2
votes

I created a new NestJS project which is a very popular NodeJS framework. But I have this error (see title) on my IDE (PhpStorm 2020.2-Beta) and ESLint doesn't work at all.

I've used the NestJS CLI :

nest new nestjs-micro

I don't seem to be the only one with this problem, so it would be nice to find the cause of this problem and fix it once and for all.

I already have an open issue but I haven't had an answer, this is really very problematic.

If anyone has an idea on how to fix the problem and keeping an ESLint / Prettier integration with PhpStorm, thanks.

Repro

// .eslintrc.js
module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: 'tsconfig.json',
    sourceType: 'module',
  },
  plugins: ['@typescript-eslint/eslint-plugin'],
  extends: [
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
    'prettier',
    'prettier/@typescript-eslint',
  ],
  root: true,
  env: {
    node: true,
    jest: true,
  },
  rules: {
    '@typescript-eslint/interface-name-prefix': 'off',
    '@typescript-eslint/explicit-function-return-type': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
  },
};

// tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  }
}

Additional Info Screenshot_20200716_233543

Versions

Typescript: 3.7.4
Node: 14.3.0
ESLint: 7.1.0
@typescript-eslint/parser: 3.0.2
Yarn: 1.22.4
3

3 Answers

7
votes

I figured it out.

The error occurs when Typescript does not have a file to include for compilation.

The simplest solution is to create a tsconfig.build.json file for example and specify the following parameters in it :

{
  "extends": "./tsconfig.json",
  "exclude": ["node_modules", "test", "dist", "dist/**/*spec.ts"],
  "include": ["src/**/*", ".eslintrc.js"]
}

The above example is adapted for NestJS but should work for other projects.

The most surprising thing is that it's only an error that shows up on PHPStorm, the build, it works fine.

7
votes

FWIW what worked for me with this error was to add ignorePatterns: ['.eslintrc.js'], to the .eslintrc.js file. This line tells eslint to ignore the .eslintrc.js file (since it's not included in the tsconfig declared project rootDir).

2
votes

Add it to the includes in tsconfig.json:

"include": [
  ".eslintrc.js",
]