51

In my typescript project, I am using eslint. The files below are in the root, and I have also subfolders /dist and /src.

eslintrc.js

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  parserOptions: {
    tsconfigRootDir: __dirname,
    project: ['./tsconfig.json'],
  },
  rules: {
    strict: 'error',
    semi: ['error', 'always'],
    'no-cond-assign': ['error', 'always'],
  },
  plugins: ['@typescript-eslint'],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
  ],
}

tsconfig.json

{
  "compilerOptions": {
    "outDir": "dist",
    "noImplicitAny": true,
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "module": "ES2020",
    "target": "ES2020",
    "lib": ["ES2020"],
    "allowJs": false,
    "alwaysStrict": true
  },
  "include": ["src"]
}

the word module on top has a red line with this 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

How can I fix this?

Wenfang Du
  • 5,689
  • 6
  • 40
  • 66
omega
  • 35,693
  • 74
  • 215
  • 425

3 Answers3

78

Update your eslintrc.js to include:

ignorePatterns: ['.eslintrc.js']

Wenfang Du
  • 5,689
  • 6
  • 40
  • 66
Joe Lawson
  • 1,169
  • 8
  • 8
53

What is this error about?

The annoying error is basically saying the file eslintrc.js itself is both:

  1. Found by ESLint, but
  2. Doesn't match the list of files it's supposed to lint

So, ESLint doesn't know what to do and freaks out!

Note that while this file doesn't include your codes, you should decide whether you want this file to be linted (e.g. for tab vs space, or double quote vs single quote,) or not.


What should be done?

You can omit either (1) or (2) from the conflicting situation above.

Solution 1: Tell ESLint to ignore the file

It's easier. Just add the name of the file (e.g. .eslintrc.js) to .eslintignore.

You can also see case 1.3 of my answer, here for different ways it can be done.

  • Pros: Quick. Easy. Painless.
  • Cons: The formatting of this file can become inconsistent with other files in terms of tab/space or quotes style.

Solution 2: Tell ESLint to lint this file (Preferred)

Typescript-ESLint gets the list of files to include from tsconfig.json via parserOptions > project.

So, you can either add the file to the existing tsconfig.json (Solution 2.1) or create a secondary tsconfig.eslint.json to include files like this, that you want to be linted by typescript-eslint, but not necessarily processed by the Typescript compiler.

  • Pros: This file, and other similar .js configs will also be included for lining and keeping everything consistent.
  • Cons: You might not want these configs files to be linted. Also, it takes a bit extra work anyway.

Solution 2.1: add it to the existing tsconfig.json

// in tsconfig.json ...

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

Solution 2.2: add it to a separate tsconfig.eslint.json file Create a file names tsconfig.eslint.json with this content:

// new file: tsconfig.eslint.json

{
    "include": [
        ".eslintrc.js"
    ]
}

and then inject it into eslintrc.js's parserOptions > project (yes, project accepts both a string, and an array of strings) :

// in eslintrc.js ...

  parserOptions: {
    project: [
        resolve(__dirname, './tsconfig.json'),
        resolve(__dirname, './tsconfig.eslint.json'),
    ],
  }

PS. This answer is very similar, I shall give some credit to that.

Aidin
  • 15,478
  • 3
  • 57
  • 51
  • 2
    Thanks, adding `.eslintrc.js` to the project explicitly solved the issue for me. I thought my glob patterns should have covered it, but apparently they didn't. – Alexey Orlenko Aug 31 '21 at 18:43
  • 1
    Fantastic explanation that makes things clear, thanks a lot! – A Mehmeto Nov 06 '21 at 09:22
  • 1
    This should be the preferred answer right now. Though, now it makes me wonder should I just ignore "File is a CommonJS module; it may be converted to an ES module.ts(80001)" or is there a way to specify that these config files should be CommonJS modules – tettoffensive Mar 24 '22 at 16:22
-2

same error, this worked for me: https://github.com/nestjs/nest/issues/4900#issuecomment-669743374 https://github.com/nestjs/nest/issues/4900#issuecomment-707330674

looks like config file must be named .eslintrc and use json format instead of .eslintrc.js and parserOptions.createDefaultProgram must be setted to true

  • It's probably `createDefaultProgram` that 'fixed' it but that is a lot more memory intensive: https://github.com/typescript-eslint/typescript-eslint/issues/967#issuecomment-539091676 – Sjeiti Feb 18 '21 at 09:57