nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.74k stars 7.63k forks source link

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

Closed OlegBrony closed 4 years ago

OlegBrony commented 4 years ago

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?

kamilmysliwiec commented 4 years ago

I can't reproduce your issue. Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

OlegBrony commented 4 years ago

I can't reproduce your issue. Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

ok, but what should I looking for? looks like I don't get relevant result when I google Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.

edit: well, I did some researches. first, eslint is working. I can't understand where is the bug. web storm says that: image but

 $ npm run lint

> hello-nest@0.0.1 lint /home/user/work/projects/hello-nest
> eslint "{src,apps,libs,test}/**/*.ts" --fix

but if I do something like that: project: '../tsconfig.json',, then

 $ npm run lint

> hello-nest@0.0.1 lint /home/user/work/projects/hello-nest
> eslint "{src,apps,libs,test}/**/*.ts" --fix

/home/user/work/projects/hello-nest/src/app.controller.spec.ts
  0:0  error  Parsing error: Cannot read file '/home/user/work/projects/tsconfig.json'

so, eslint really trying to find the tsconfig file, and he can found it, but ide is showing error. how to fix that? or it doesn't matter? lint works anyway.

heleg commented 4 years ago

@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.

BrenoGO commented 4 years ago

I believe this issue should be a bug.. I am having the same error. The script lint runs but doesn't fix or complain about any of the many linting errors I left in the code...

jmcdo29 commented 4 years ago

@BrenoGO can you provide a minimum reproduction?

BrenoGO commented 4 years ago

Just run "nest new my-project" ("@nestjs/cli": "^7.0.0",). The .eslintrc will show the error of this issue's title. I ran the eslint --init, now I will configure it. I thought it would start already configured.

BrenoGO commented 4 years ago

I am using the nest 7.4.1. Using the git clone https://github.com/nestjs/typescript-starter.git, it's the same.. Does anyone does these procedures and the eslint works fine?! Could be something with my VSCode? In other projects I always use it configuring from beginning with eslint --init and is working fine..

jmcdo29 commented 4 years ago

@BrenoGO using the latest version of the Nest CLI I created a new project

$ nest new nest-lint-problem -p yarn

Once the installation finished I moved into the new project's directory

$ cd nest-lint-problem

And then I ran the linter

$ yarn lint
eslint "{src,apps,libs,test}/**/*.ts" --fix
Done in 13.18s.

Just to be sure, I then ran eslint directly

yarn eslint --fix
~/nest-lint-problem/node_modules/.bin/eslint --fix
Done in 3.33s.

Not sure what you're facing, but the configuration setup is fine in terms of running the lint command. It sounds like the problem is with the IDE, as in eslint trying to lint the .eslintrc.js file. As that doesn't really happen from the lint commands, I don't really see it being an issue. If you want to fix it though, you can create an .eslintignore file and add all .js files or specific ones, just like you would for a .gitignore

BrenoGO commented 4 years ago

The problem is when we go to a file and leave some code out of pattern, the eslint does not report any errors.. Screen Shot 2020-07-24 at 06 41 52

I thought this should be not happening as expected because of the error in the .eslintrc below: Screen Shot 2020-07-24 at 06 44 42

sanch941 commented 4 years ago

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

BrenoGO commented 4 years ago

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. It seems like we have to configure it all by ourselves..

sanch941 commented 4 years ago

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

BrenoGO commented 4 years ago

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

There we go! thanks

lucasluca commented 4 years ago

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

BrenoGO commented 4 years ago

@lucasluca , did you also did that?

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

what did not work for you?

hreisi commented 4 years ago

I did exactly the same thing. Added the packages and extended what was recommended. I still have the same issue.

hreisi commented 4 years ago

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

OlegBrony commented 4 years ago

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

stvnwrgs commented 4 years ago

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
  },
  ...
ghost commented 3 years ago

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.

jmcdo29 commented 3 years ago

The lint command that Nest provides in the package.json works fine, and I can't reproduce this on VSCode unless I open up the .eslintrc.js file. However, I do see where this is coming from, so if you'd like to create a PR to the typescript-starter to add that line in, please feel free to do so. I'll double check if any other locations would need it as well.

huybuidac commented 3 years ago

"createDefaultProgram": true worked, don't know why @@