354

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

Paolo
  • 20,112
  • 21
  • 72
  • 113
Dan
  • 8,041
  • 8
  • 41
  • 72
  • 8
    Add `babel.config.js` in the `tsconfig.js` file: "include": [ "next-env.d.ts", "**/*.ts", "**/*.tsx", "postcss.config.js", ".eslintrc.js" ] – Himanshu Tanwar Jul 27 '20 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](https://stackoverflow.com/a/64488474/8839059) for another approach, parsing only TS files. – Rafael Tavares Oct 22 '20 at 18:45
  • The answer from this thread helped me: https://stackoverflow.com/questions/63879049/what-means-parsing-error-parseroptions-project-has-been-set-for-typescript – ncux199rus Feb 21 '23 at 14:57

27 Answers27

298

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.

Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
  • This is basically the solution, working perfectly, explanation justifies solution. – Amit Dec 26 '20 at 17:49
  • it is pretty clear from comments, yet someone interested in understanding `overrides` attribute of eslint https://eslint.org/docs/user-guide/configuring#disabling-rules-only-for-a-group-of-files – Amit Dec 26 '20 at 17:51
  • 8
    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'], }, }` – Michael Große Dec 28 '20 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 '21 at 09:59
  • 3
    @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. – Rafael Tavares Feb 25 '21 at 12:22
  • 2
    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 03 '23 at 14:37
115

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
Željko Šević
  • 3,743
  • 2
  • 26
  • 23
  • 4
    You don't need to include that file, just don't parse it. Take a look at [my answer](https://stackoverflow.com/a/64488474/8839059), I explained it and shared a code to solve that problem :) – Rafael Tavares Oct 22 '20 at 18:38
  • 1
    @RafaelTavares but if I want to lint configs? – godblessstrawberry Jun 04 '21 at 11:00
  • 1
    @godblessstrawberry you shouldn't lint `.js` with TypeScript linting. Just use JS ESLint rules. – Rafael Tavares Jun 04 '21 at 11:02
  • @RafaelTavares how? – SalahAdDin Aug 11 '21 at 22:26
  • Thanks. I had the same problem but with a "test" folder that contains my unit tests and is not part of my "src" folder which contains the actual code that is compiled by TSC. – Krisztián Balla Sep 25 '21 at 20:20
  • 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. – JeneralJames Dec 16 '21 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 07 '22 at 10:04
86

Add one line in ".eslintignore":

.eslintrc.js

Yoel
  • 7,555
  • 6
  • 27
  • 59
magentaqin
  • 1,949
  • 13
  • 10
  • 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 '21 at 12:03
  • 83
    For gods sake. This absolute mess of plugins, extensions, language versions, compilers version and stuff is driving me crazy already. – José Cabo Mar 08 '22 at 10:28
  • 2
    Can you please explain your answer? – YTG Jul 13 '22 at 06: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 '22 at 17:44
30

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.

codejockie
  • 9,020
  • 4
  • 40
  • 46
  • 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 '20 at 17:54
27

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

erikdstock
  • 1,117
  • 9
  • 16
  • 14
    but what if I want it to be ignored? It is still showing if I add it in exclude array.. – Dipesh Raichana Dec 28 '19 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 '19 at 21:44
  • There may be more helpful context which i should check out too [here](https://github.com/typescript-eslint/typescript-eslint/tree/b1c8c0a5889f0db1a96e90b512288553f68cc7ee/packages/parser) – erikdstock Dec 29 '19 at 21:47
  • 2
    You don't need to include that file, just don't parse it. Take a look at [my answer](https://stackoverflow.com/a/64488474/8839059), I explained it and shared a code to solve that problem :) – Rafael Tavares Oct 22 '20 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, }, ``` – Atsuhiro Teshima Dec 08 '20 at 15:54
  • the other answers are talking about js vs ts, but this answer solves the issue when you have this error on a ts file, that was my case, thank you! – shamaseen Apr 02 '22 at 20:02
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!

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
  • 1
    For me just adding the .config to include resolved the issue. Final fixed version --> ` "include": ["next-env.d.ts", "\*\*/*.ts", "\*\*/*.tsx", "\*\*/*.config.js"],` – just-be-weird Nov 07 '21 at 16:12
  • 2
    In my case, adding `"**/*.eslintrc.js"` and restarting the IDE fixed it. Thanks. – Ryan Jan 24 '22 at 21:24
14

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.

Sebastian Kropp
  • 366
  • 2
  • 8
11

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

ignorePatterns: ['.eslintrc.js']
adi1ya
  • 404
  • 1
  • 7
  • 10
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

Ali80
  • 6,333
  • 2
  • 43
  • 33
5

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',
  },
}
Wenfang Du
  • 8,804
  • 9
  • 59
  • 90
  • 3
    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 '21 at 07:15
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.

greuze
  • 4,250
  • 5
  • 43
  • 62
jony89
  • 5,155
  • 3
  • 30
  • 40
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.

Waldemar Lehner
  • 875
  • 10
  • 8
  • 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. – Sean Dawson Nov 14 '21 at 02: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 '23 at 17:57
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.

btd1337
  • 2,616
  • 2
  • 16
  • 25
  • [Link to the respective GitHub issue](https://github.com/typescript-eslint/typescript-eslint/issues/2987) – phil294 May 08 '22 at 16:05
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.

Caleb Jay
  • 2,159
  • 3
  • 32
  • 66
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.

Freewalker
  • 6,329
  • 4
  • 51
  • 70
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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
dankoiDev
  • 63
  • 5
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.

user9027325
  • 135
  • 2
  • 6
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": [""]
}
Sh eldeeb
  • 1,589
  • 3
  • 18
  • 41
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.

preet kaur
  • 11
  • 2
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"],
Hossein Mousavi
  • 3,118
  • 3
  • 16
  • 35
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 :)

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

Alisson Leal
  • 154
  • 1
  • 5
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 :\

shamaseen
  • 2,193
  • 2
  • 21
  • 35
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.

three
  • 8,262
  • 3
  • 35
  • 39
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.

Zyncho
  • 407
  • 6
  • 12
-4

Set parserOptions in .eslintrc as below

"parserOptions": {
  "ecmaFeatures": {
    "jsx": true
  },
  "ecmaVersion": 2019,
  "sourceType": "module"        
},
Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48