2

I am getting this error when i do eslint .

Error: Failed to load parser '@typescript-eslint/parser' declared in '.eslintrc.yml » 
eslint-config-react-app#overrides[0]': Cannot find module 'typescript'
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497

2 Answers2

5

Because there are TypeScript files - although ESLint excludes node_modules/ from linting by default, CRA's override (in order to load appropriate plugins for any TypeScript files, this is why you see eslint-config-react-app#overrides[0] in the output) does include the content of node_modules/ (see e.g. this issue).

You can install TypeScript, but if you don't intend to use any TypeScript files in your own source you can be more specific about what you want to lint instead. Either:

  1. Change the script in package.json to only lint the source directory:

    "lint": "eslint src/"
    
  2. Create a .eslintignore (or another file, if you set the right --ignore-path) file containing:

    node_modules/
    
  3. Add to the ESLint configuration object in package.json (or pass it as an --ignore-pattern):

    "eslintConfig": {
      "extends": "react-app",
      "ignorePatterns": [
        "node_modules/"
      ]
    }
    

The latter two options are from the documentation on configuration.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • @DmitryGrinko any chance you could expand on that a little bit? None of those three options make any difference to the outcome? One or more of them leads to a different outcome that's still not what you hoped for? – jonrsharpe Nov 17 '20 at 23:53
  • @DmitryGrinko that appears to be a different error than the one mentioned in the question, and as far as I'm aware CRA doesn't use `eslint-config-airbnb-typescript`. If you're using some other template, that would be a separate question entirely. – jonrsharpe Nov 17 '20 at 23:58
  • I'm not using CRA. I'm trying to install eslint on SLS+TS – Dmitry Grinko Nov 18 '20 at 00:00
  • @DmitryGrinko I don't know what SLS+TS is, but this question is about a problem caused by CRA's built-in ESLint configuration. If you're not using that, then I wouldn't necessarily expect these answers to work, because that's potentially an unrelated issue. – jonrsharpe Nov 18 '20 at 00:02
  • It's a Serverless framework with Typescript. The same problem. The .eslintignore file doesn't ignore node_modules – Dmitry Grinko Nov 18 '20 at 00:04
  • 1
    @DmitryGrinko but if you're actually using TS then the relevant dependencies should be installed already and you wouldn't see this at all. `eslint-config-airbnb-typescript` that has a peer dep on `@typescript-eslint/parser` and `typescript` via `@typescript-eslint/eslint-plugin`. This issue's about a case where TypeScript isn't installed because it isn't needed - the OP's source is JS. Yours seems unrelated, I'd suggest asking a separate question (and checking you have all the right peer deps installed - `npm ls`, see https://stackoverflow.com/a/63177495/3001761). – jonrsharpe Nov 18 '20 at 00:08
1

Install typescript

npm install --save-dev typescript

Then eslint .

will work as intended and can use the .eslintrc.yml file.

btw I had also just installed

npm install -save-save eslint-plugin-typescript

in case you are doing this as you may need that too.

btw do NOT install eslint with npm install eslint... as it may mess up due to the fact that... crewate-react-app already has it (but a lower version). so just add your eslintrc.yml file and it will work (be applied in the editor, and with this fix, at the command line as neeeded).

example output:

.../src/components/SignUp/index.js
   1:41  error  Missing semicolon                              semi
   7:26  error  Unnecessary parentheses around expression      no-extra-parens
  15:2   error  Missing semicolon                              semi
  32:13  error  'username' is assigned a value but never used  no-unused-vars
  47:63  error  Missing semicolon                              semi
  95:26  error  Unnecessary parentheses around expression      no-extra-parens
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497