375

I created a new React Native project with --template typescript

I deleted the template directory which came as part of the boilerplate.

I then proceeded to add ESLint:

module.exports = {
  parser: "@typescript-eslint/parser",
  plugins: ["@typescript-eslint"],
  extends: ["airbnb-typescript-prettier"]
};

However, when I open babel.config.js, I get this error

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

The file does not match your project config: /Users/Dan/site/babel.config.js.

The file must be included in at least one of the projects provided.eslint

3
  • 8
    Add babel.config.js in the tsconfig.js file: "include": [ "next-env.d.ts", "*/.ts", "*/.tsx", "postcss.config.js", ".eslintrc.js" ] Jul 27, 2020 at 10:49
  • 3
    @HimanshuTanwar no need to include babel.config.js, just don't parse it with ESLint TypeScript parser. Take a look at my answer for another approach, parsing only TS files. Oct 22, 2020 at 18:45
  • The answer from this thread helped me: stackoverflow.com/questions/63879049/…
    – ncux199rus
    Feb 21, 2023 at 14:57

29 Answers 29

326

Different lint rules for JavaScript and TypeScript files

The problem happens for one of the reasons below:

  1. You're using a rule which require type information and didn't specify a parserOptions.project;
  2. You specified parserOptions.project, didn't specify createDefaultProgram (it will be removed in a future version), and you're linting files not included in the project (e.g. babel.config.js, metro.config.js)

As from the TypeScript ESLint Parser docs:

parserOptions.project

This option allows you to provide a path to your project's tsconfig.json. This setting is required if you want to use rules which require type information.

(...)

Note that if this setting is specified and createDefaultProgram is not, you must only lint files that are included in the projects as defined by the provided tsconfig.json files. If your existing configuration does not include all of the files you would like to lint, you can create a separate tsconfig.eslint.json.

To solve it, update your ESLint config to use TypeScript rules only on TypeScript files:

{
  // ...
  parser: '@typescript-eslint/parser',
  plugins: ["@typescript-eslint"],
  overrides: [
    {
      files: ['*.ts', '*.tsx'], // Your TypeScript files extension

      // As mentioned in the comments, you should extend TypeScript plugins here,
      // instead of extending them outside the `overrides`.
      // If you don't want to extend any rules, you don't need an `extends` attribute.
      extends: [
        'plugin:@typescript-eslint/recommended',
        'plugin:@typescript-eslint/recommended-requiring-type-checking',
      ],

      parserOptions: {
        project: ['./tsconfig.json'], // Specify it only for TypeScript files
      },
    },
  ],
  // ...
}

You can read more about the overrides config on the official docs: How do overrides work?


Don't lint a specific file

If you don't want to lint the file that is mentioned in the error (e.g. babel.config.js), you can ignore it adding its name to the .eslintignore file:

babel.config.js

Anyway, the step above (about overriding the config for TypeScript files) is important in case your project contains both JavaScript and TypeScript files that you want to lint.

You can also create other overrides for different situations, e.g. a different config for test files, since it can use developer dependencies and run on node environment, instead of browser.

6
  • 1
    This is basically the solution, working perfectly, explanation justifies solution.
    – Amit
    Dec 26, 2020 at 17:49
  • 9
    This doesn't work in the second case as it still tries to apply the rule needing the type information to the .js file, e.g. babel.config.js. What is needed is to also add the rules that require the types to this overrides clause. So, e.g.: { files: ['*.ts'], extends: [ 'plugin:@typescript-eslint/recommended-requiring-type-checking', ], parserOptions: { project: ['./tsconfig.json'], }, } Dec 28, 2020 at 13:19
  • 6
    Isn't putting such files in .eslintignore the cleanest solution? Linting configuration files doesn't really add benefit. And rewriting the ESLint config in a way using overrides to only pick up desired files feels weird -- doesn't it just emulate an "ignore" but with more complexity?
    – bluenote10
    Feb 25, 2021 at 9:59
  • 5
    @bluenote10 I use both overrides.rules and rules because I need to lint different files in different ways. For example, rules contains the rules that I want to apply to all files (both .ts and .js), while I have 2 different overrides to apply TypeScript rules (@typescript-eslint-) and some specific rules to Test files (e.g. allow devDependencies in import/no-extraneous-dependencies rule). I don't want to ignore files, just to have different rules according to its needs. Feb 25, 2021 at 12:22
  • 3
    God bless you and blessed be the womb that delivered you because this worked and I felt like saying this and thank you and thank you and I won't be resigning now to go work in McDonald's.
    – Mina
    Mar 3, 2023 at 14:37
117

You can create a separate TypeScript config file (tsconfig.eslint.json) intended for eslint configuration. That file extends tsconfig configuration and setups include key for files that have to be linted.

.eslint.js:

// ...
parserOptions: {
  // ...
  project: "./tsconfig.eslint.json",
  // ...
},
// ...

tsconfig.eslint.json:

{
  "extends": "./tsconfig.json",
  "include": [
    // ...
    "babel.config.js"
  ]
}

Or if you want to ignore it, you can put it into .eslintignore.

.eslintignore:

// ...
babel.config.js
7
  • 4
    You don't need to include that file, just don't parse it. Take a look at my answer, I explained it and shared a code to solve that problem :) Oct 22, 2020 at 18:38
  • 1
    @RafaelTavares but if I want to lint configs? Jun 4, 2021 at 11:00
  • 1
    @godblessstrawberry you shouldn't lint .js with TypeScript linting. Just use JS ESLint rules. Jun 4, 2021 at 11:02
  • 1
    Doesn't work. I created a tsconfig.eslint.json, extending from tsconfig.json and added the file I wish to include but I still get the same error. Dec 16, 2021 at 18:30
  • 2
    This is exactly what I needed. I didn't want to include the files into some existing config because they're swapped at build time and would only increase final bundle for no reason but at the same time I didn't want to drop the lint on these files. One thing to add: If the config you extends from has an exclude array, you may want to override that to be an empty array or something different. Took me a bit of time to figure out why my "include": ["**/*.ts"] wasn't working. Thanks a lot Zeljko for sharing
    – maxime1992
    Feb 7, 2022 at 10:04
89

Add one line in ".eslintignore":

.eslintrc.js

4
  • 1
    i had to ignore a different file, the one it was complaining about, some HTML file, but this approach worked +1
    – danday74
    Apr 30, 2021 at 12:03
  • 97
    For gods sake. This absolute mess of plugins, extensions, language versions, compilers version and stuff is driving me crazy already.
    – José Cabo
    Mar 8, 2022 at 10:28
  • 2
    Can you please explain your answer?
    – YTG
    Jul 13, 2022 at 6:59
  • 3
    @YTG I'll explain it. If you have parserOptions.project set to @typescript-eslint/parser then I guess your project is written in Typescript, right? But when applied to regular js-files it may break now. .eslintrc.js itself is a js file too. Which means your eslint config cannot be applied to itself correctly.
    – Gherman
    Jul 15, 2022 at 17:44
31

In my ESLint config I have the following configuration:

.eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true,
  },
  globals: {
    Promise: "readonly"
  },
  parser: "@typescript-eslint/parser",
  parserOptions: {
    sourceType: "module",
    tsconfigRootDir: __dirname,
    project: ["./tsconfig.eslint.json"],
  },
  plugins: ["@typescript-eslint"],
  extends: [
    "eslint:recommended",
    "prettier/@typescript-eslint",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking",
  ],
}

Update The key part of the fix is this:

parser: "@typescript-eslint/parser"

and this:

parserOptions: {
    sourceType: "module",
    tsconfigRootDir: __dirname,
    project: ["./tsconfig.eslint.json"], // could be tsconfig.json too
}

Don't forget to include that file or file's directory in the include array of tsconfig.json.

1
  • 2
    This wouldn't work in case you're ~linting files not included in the project (e.g. babel.config.js, metro.config.js)_ if you are getting error like The file must be included in at least one of the projects provided.eslint then it is the case.
    – Amit
    Dec 26, 2020 at 17:54
28

You need to add that file to your tsconfig include array. See typescript-eslint/typescript-eslint#967 for more details.

6
  • 14
    but what if I want it to be ignored? It is still showing if I add it in exclude array.. Dec 28, 2019 at 15:20
  • 3
    I believe the point is that typescript+eslint relies on the @typescript-eslint parser so categorically requires linted files to be visible to ts. I'm honestly not sure about this use case. Why do you need it to be excluded? I believe the philosophy of typescript-eslint is based on the assumption that all linted files should be part of the project, and all files that are part of the project should be linted (even if they aren't a critical entrypoint for production code)- It would make sense for example to want type errors if in this case you were specifying a bad babel config.
    – erikdstock
    Dec 29, 2019 at 21:44
  • There may be more helpful context which i should check out too here
    – erikdstock
    Dec 29, 2019 at 21:47
  • 2
    You don't need to include that file, just don't parse it. Take a look at my answer, I explained it and shared a code to solve that problem :) Oct 22, 2020 at 18:35
  • 1
    I added the following to tsconfig.json and the issue solved. "include": ["./src/**/*ts", "./src/**/*.tsx"], // part of .eslintrc ``` parser: '@typescript-eslint/parser', parserOptions: { project: './tsconfig.json', sourceType: 'module', createDefaultProgram: true, }, ``` Dec 8, 2020 at 15:54
22
  1. Run npm i -D @types/node

  2. Include typescript support for *.config.js files:

tsconfig.json:

{
  "include": [
    "**/*.config.js" // for *.config.js files
  ]
}

And then reload your IDE!

2
  • 1
    For me just adding the .config to include resolved the issue. Final fixed version --> ` "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.config.js"],` Nov 7, 2021 at 16:12
  • 2
    In my case, adding "**/*.eslintrc.js" and restarting the IDE fixed it. Thanks.
    – Ryan
    Jan 24, 2022 at 21:24
15

Simply instruct eslint to ignore them by adding the ignorePatterns option to your .eslintrc.js:

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: './tsconfig.json',
  },
  ignorePatterns: ["babel.config.js"],
  ...

As there is not much value in linting your project config files, you can safely ignore them.

Also, I would not make them part of your tsconfig.json or create a special tsconfig.eslint.json file, just for linting purpose.

1
  • 1
    I think this is the best solution.
    – zelanix
    May 25, 2022 at 17:36
11

Simply add below code inside the .eslintrc.js file.

ignorePatterns: ['.eslintrc.js']
8

The error means there is a file which can be compiled but doesnt belong to currently defined project structure. there are several solutions:

If you dont care if the file is compliled (ie. config files and such)

  • add the filename to .eslintignore

or

  • add the file or file pattern to .eslintrc.js file
ignorePatterns: ["babel.config.js"]
// or 
ignorePatterns: ["**/*.config.js"]

You want the file to compiled

add the file, folder or pattern to tsconfig.json file

include": [
    "src",
    "other_src",
]

NOTE some changes needs IDE restart to take effect

6

I was having the same issue when adding '@typescript-eslint/prefer-nullish-coalescing': 'error' to .eslintrc.cjs, the following setup worked for me:

// tsconfig.json
{
  // ...
  "include": ["**/*", "**/.*"],
  "exclude": ["node_modules", "dist"]
}
// .eslintrc.cjs
module.exports = {
  // ...
  parser: '@typescript-eslint/parser',
  parserOptions: {
    // this setting is required to use rules that require type information
    project: true,
  },
  rules: {
    '@typescript-eslint/prefer-nullish-coalescing': 'error',
  },
}
1
  • 4
    Thanks, solved my problem: 👍🏼 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.
    – ru4ert
    Aug 22, 2021 at 7:15
4

Does this Error come from the VSCode Extension? Does the Error have a relative Path that goes to the top level even though its in the same folder (or subfolder) (as shown in the error message below)?

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: ../../../../../home/<ommitted>/projects/floor-planner/app/src/App.vue.
The file must be included in at least one of the projects provided.

If so, check if it also persists when using the CLI.

From your project root you could run eslint .. If those errors are not found, you very likely have the project opened through a symlink.

As of Aug 2021, the Eslint Extension does not work with Symlinks.

From your project directory, run pwd -P to get the absolute Path.

In my instance this results in

~/projects/floor-planner/app: pwd -P
/media/projects/2021-Q3/floor-planner/app

Open the Project through this Path.

Eslint should no longer show the Error.

2
  • 2
    Anyone having this issue in Intellj with multi module projects and use Fedora Silverblue, it turns out that the home folder is actually a symlink. /home/foobar is a symlink to /var/home/foobar. If you open the project folder in Intellj via the ~ or /var/home it seems to work properly. Nov 14, 2021 at 2:46
  • 1
    Thx, it help me a lot. I run Intellij IDEA on Ubuntu 22.04 with zfs, and create a symlink to my project files that saved in my data pool.
    – Juxuny Wu
    Jan 16, 2023 at 17:57
4

For me the problem originates in some files ignored in tsconfig using the exclude property, yet eslint does not ignore them.

Usually if one wants that TSC will ignore files, then the same will be applied to eslint. so just copy paste the value of exclude in .tsconfig configuration into the ignorePatterns property in .eslintrc configuration.

3

I use a symlink from my project folder point to my home folder:

/opt/project/workspace => ~/worspace

Opening the VSCode using the symlink path ~/workspace the error always persists.

Opening VSCode using the original path: /opt/project/workspace solves the problem.

This shouldn't be a problem, but for now it is.

1
2

I needed just to add the path to the new tsconfig.json in the project array and eslint script and the issue went away.

tsconfig.json:

project: ["./newfolder/tsconfig.json"]

package.json:

"eslint": "eslint -c ./.eslintrc.js './newfolder/**/*.ts'"
2

Despite assurances that typescript definitely, absolutely, never caches anything, I suspected this was a caching issue after, when getting annoyed at incessant typescript errors while debugging and developing a feature, I modified my tsconfig.json to exclude my src directory temporarily. Upon reverting the change, and restarting my typescript server running through WebpackDevServer, I had this error, and got rid of it by doing yarn cache clean to clear my yarn cache, as well as rm -rf node_modules to delete my node modules folder. Then I re installed my packages with yarn and when I ran my devserver again, typescript/tslint no longer showed this error.

1

In our case we have multiple .eslintrc and multiple tsconfig.json in our directory tree. (One each for the server at ., and one each for the React client at ./client.)

This worked fine with the CLI but not with VS Code's plug-in.

The fix was to set the ./client/.eslintrc project value to be relative to the root - not to the .eslintrc file. After changing it from "./tsconfig.json" to "./client/tsconfig.json" the IDE linting works as expected.

1

I had this issue in VsCode when I was opening my Project too high up in the Folder Structure. A weird solution but it worked.

In my example, Firestore functions project for express in .ts, I had to open it from the functions folder it created.

I should have noticed it was this sort of problem since 'npm run-script lint' never returned an error itself.

1

I ran into this issue today, and none of the above solutions helped. The issue I had was that I had two files in the same directory that shared the same file name (sans extension). E.g., src/foo.ts and src/Foo.tsx. Renaming one of the files fixed the linter error. See @typescript-eslint/parser#955.

1

for me, this issue happens when the project listed in parserOptions.project extends the base tsconfig.json which excludes this file.

removing extends: './tsconfig.json' from the project listed in parserOptions.project or removing exclude from the base config fixes it. but this workaround is not practical as we need to extend the base config.

overriding the property exclude will perfectly help.

tsconfig.spec.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./dist",
    "types": ["jest"]
  },
  "include": ["**/*.spec.ts"],
  "exclude": [""]
}
1

After putting lots of efforts and trials in correcting this error.I got to know my issue is i saved the ignore file as eslintignore instead of .eslintignore.

So due to this it was not able to ignore the .js, .d.ts files as dist got ignored.

0
1

I happen to have the same problem for a while and then after adding .html to the include array of my tsconfig.json file, the problem has been fixed.

Since I am working on an Angular project, in the files array inside the overrides you may see my components extension which you may need to change as your project necessity.

in the eslintrc.json file you need to have the following configuration:

    "extends": [
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:@typescript-eslint/recommended-requiring-type-checking",
        "plugin:@angular-eslint/recommended"
    ],
    "overrides": [
        {
            "files": ["*.ts", "*.component.html", "*.component.ts"],
            "parserOptions": {
                "project": ["./tsconfig.json"],
                "extraFileExtensions": [".html"]
            },
            "rules": { "@typescript-eslint/prefer-nullish-coalescing": "error" }
        }
    ],
    "parser": "@typescript-eslint/parser",

make sure that you have the following line in your tsconfig.json file:

"include": ["**/*.d.ts", "**/*.ts", "**/*.html", "**/*.scss", "**/*.css"],
1

Linux - My projects directory /home/user/projects was a symlink to /usr/share/dev/projects. I removed the symlink and everything works now.

The error I was getting was telling me it couldn't find any .tsx file in "../../../../../{app name/path}"

I hope my hours of troubleshooting helps someone :)

1

If you do NOT want to lint the file with type-aware linting, in @typescript-eslint v6, you can apply a new disable type-aware rules config for .js files. It's the opposite of the popular solution of adding type-aware rules only to override for .ts files, and it's probably more convenient and straightforward. You can also read the docs about it here.

...
overrides: [
    {
      extends: ['plugin:@typescript-eslint/disable-type-checked'],
      files: ['./**/*.js'],
    },
  ],

0

I spent a lot of time on a problem like this one.

I had created a jest.config.ts instead of jest.config.js so it was throwing this exact eslint error🤦

0

My issue was in PHPStorm is that I was having the working director set:

enter image description here

I cleared that and everything worked :\

0

I had this error thrown at me even with the project settings included. It was a vuesfc and the solution was a missing <script></script> template.

0

By default Eslint "Controls whether eslint is enable or not."

To Disable this option go to Settings > search for eslint > Eslint:Enable (uncheck the box: Controls whether eslint is enable or not.)

Hope it helps.

0

adding to .eslintrc.js works fine:

parserOptions: {
    project: 'tsconfig.eslint.json',
}
-4

Set parserOptions in .eslintrc as below

"parserOptions": {
  "ecmaFeatures": {
    "jsx": true
  },
  "ecmaVersion": 2019,
  "sourceType": "module"        
},

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.