nest: eslint: Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.

Here is the fix

https://github.com/nestjs/nest/issues/4900#issuecomment-669743374

Bug Report

I installed nest and start new project. and there is a problem with eslint: in .eslint.js I got error

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: .eslintrc.js.
The file must be included in at least one of the projects provided.

eslint in code also not working, and command npm run lint doesn’t find any problems.

Current behavior

Input Code

.eslintrc.js

module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    sourceType: 'module',
    project: 'tsconfig.json'
  },
  plugins: ['@typescript-eslint/eslint-plugin'],
  extends: [
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
    'prettier',
    'prettier/@typescript-eslint',
  ],
...
};

I didn’t change anything actually.

Environment


Nest version: 7.2.0
 
For Tooling issues:
- Node version: 13.10.1
- Platform:  Linux mint

webstorm, npm 6.13.7.

is it reproduced or is it just me?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 7
  • Comments: 22 (4 by maintainers)

Commits related to this issue

Most upvoted comments

In my opinion, NestJS creates an erroneous configuration.

The file .eslintrc.js shoud look like this:

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

By adding ignorePatterns: ['.eslintrc.js'] eslint is told to ignore the .eslintrc.js file itself.

Otherwise it results in an error, because eslint tries to lint the .eslintrc.js file, even though it is not included in the tsconfig.json configuration.

See also https://github.com/typescript-eslint/typescript-eslint/issues/967#issuecomment-530907956.

Adding "createDefaultProgram": true to the tsconfig, after doing https://github.com/nestjs/nest/issues/4900#issuecomment-669743374 fixed it for me.

  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json",
    "sourceType": "module",
    "createDefaultProgram": true
  },
  ...

SO… all the fix was about

  1. changing name from ‘.eslintrc.js’ to ‘.eslintrc’.
  2. making json from object (and fix all json syntax errors). image

I had to change the extension of the .eslintrc.js to ts. Then it was resolved.

Doing that the error in .eslintrc will be gone. But still some basic rules like “semi”, “comma-dangle” and “quotes” are not being reported by the eslint. I seems like we have to configure it all by ourselves…

You should install this dependencies yarn add eslint-config-prettier eslint-plugin-prettier --dev or npm install eslint-config-prettier eslint-plugin-prettier --save-dev And replace extends object in .eslintrc by this

“extends”: [ “plugin:@typescript-eslint/eslint-recommended”, “plugin:@typescript-eslint/recommended”, “prettier/@typescript-eslint”, “plugin:prettier/recommended” ]

After VsCode reload should work

@OlegBrony, looks like this happened because webstorm automatically tries to apply eslint to .eslintrc.js file and parse it with typescript config (which is not working with js files). You can safely ignore it.

Doing that the error in .eslintrc will be gone. But still some basic rules like “semi”, “comma-dangle” and “quotes” are not being reported by the eslint. I seems like we have to configure it all by ourselves…

You should install this dependencies yarn add eslint-config-prettier eslint-plugin-prettier --dev or npm install eslint-config-prettier eslint-plugin-prettier --save-dev And replace extends object in .eslintrc by this

“extends”: [ “plugin:@typescript-eslint/eslint-recommended”, “plugin:@typescript-eslint/recommended”, “prettier/@typescript-eslint”, “plugin:prettier/recommended” ]

After VsCode reload should work

Does not worked for me

Solved by replacing filename from .eslintrc.js to .eslintrc and removing module.exports etc.