94

Today I run eslint,two script

 "lint-staged": "lint-staged",
 "eslint": "eslint --ext .tsx,.ts --fix ./src -c .eslintrc.js",

When I run npm run eslint // it's ok When I run npm run lint-staged // it's wrong

lint-staged's result ;

> lint-staged
  ❯ Running tasks for src/**/*.tsx
    ✖ eslint --fix [FAILED]
    ◼ git add
  
✖ eslint --fix :


Oops! Something went wrong! :(

ESLint: 7.10.0

Error: Error while loading rule '@typescript-eslint/dot-notation': You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.
Occurred while linting /Users/***/components/BrowserInfo/Index.tsx
    at Object.getParserServices (/Users/fugang/workspace/xinao/channel-desk/node_modules/_@typescript-eslint_experimental-utils@4.3.0@@typescript-eslint/experimental-utils/dist/eslint-utils/getParserServices.js:26:15)

In package lint-staged

What happend?

"lint-staged": {
    "src/**/*.tsx": [
      "eslint --fix -c .eslintrc.js",
      "git add"
    ]
  },
Vasiliy Artamonov
  • 939
  • 2
  • 8
  • 24
leofu
  • 941
  • 1
  • 6
  • 6

3 Answers3

202

You have to add the following config to your .eslint.json file

{
    "parserOptions": {
        ...
        "project": ["path/to/your/tsconfig/file"]
        ...
    },
}
Vladimir Jovanović
  • 5,143
  • 5
  • 21
  • 42
  • 9
    Thank you @Vladimir Jovanović. In my project, I have added like below, as "tsconfig.json" and ".eslintrc.json" are at same level. "parserOptions": { "project": ["tsconfig.json"] } – Ramya Velivala Apr 08 '21 at 03:14
  • 2
    @RamyaVelivala did you ever figure out your issue? I'm experiencing the same thing – bgmaster May 28 '21 at 15:52
  • 6
    This answered looked promising and resolved that error but resulted in this new Eslint error at the top of the `.eslintrc.js` file itself: `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.` https://stackoverflow.com/q/58510287/470749 seems related, but I haven't gotten any of those answers to work. – Ryan Jan 24 '22 at 21:19
  • 1
    This should be your '.eslintrc.js' file, but the answer still applies. – Tim Feb 04 '22 at 02:52
  • Its very interesting that official site says to add which doesnt work. But this solution works. – damdafayton Aug 12 '22 at 16:57
  • I added the project to my default file and ESLINT works in my next.js app. Incredible this isn't added by default. No tutorial even mentioned it either! – Gary Sep 05 '22 at 00:04
  • thanks man sorted me right out. error prompt should suggest this ffs – Aid19801 May 17 '23 at 11:19
9

In case anyone outhere using .eslintrc.yml can use following to fix above issue

extends:
  - airbnb-typescript
parserOptions:
  project:
    - tsconfig.json

PS: tsconfig.json is in root folder, change it to match your location in case its not in root folder.

just-be-weird
  • 1,242
  • 9
  • 7
0

In case someone has the same problem as I had: my ESLint was working perfectly fine, but my VS Code extension was throwing this error. I had to change the Extension setting and reload it in order for it to work. Basically, I did this:

"eslint.workingDirectories": [{ "mode": "auto"}],

Also explained my problem and solution here