Dan
Dan

Reputation: 8794

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

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

Upvotes: 399

Views: 499362

Answers (30)

StackemSam
StackemSam

Reputation: 21

I got this error in a Quasar Framework app that had <script setup> without specifying the language: <script lang="ts" setup>. Correcting the script tag eliminated the error.

Upvotes: 2

jpenna
jpenna

Reputation: 9161

I've been having this problem for so long and adding the files to include didn't seem to work, but then I saw I didn't accept JS files in my tsconfig...

So I added allowJS: true and it started working.

// tsconfig.json
{
  "compilerOptions": {
    ...
    "allowJs": true,
    ...
  },
  "include": ["src", "prisma/seed.ts"]
}

// tsconfig.eslint.json
{
  "extends": "./tsconfig.json",
  "include": ["src", "**/*.json", "**/*.ts", "**/*.js", "**/*.mjs"]
}

Upvotes: 2

Rafael Tavares
Rafael Tavares

Reputation: 6471

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)

ESLint flat config (version >= 9)

You can use TypeScript rules only on ts files with the following config (also, see the docs):

const tsPlugin = require('@typescript-eslint/eslint-plugin');
const tsEslint = require('typescript-eslint');

/**
 * @type {import('eslint').Linter.FlatConfig[]}
 */
module.exports = [
  // This is just an example for rules specific to JS files
  {
    files: ['**/*.js'],

    rules: {
      'no-duplicate-imports': 'error',
    },
  },

  ...tsEslint.configs.recommendedTypeChecked.map((config) => ({
    ...config,
    files: ['**/*.ts'], // We use TS config only for TS files
  })),

  {
    files: ['**/*.ts'],

    // This is required, see the docs
    languageOptions: {
      parserOptions: {
        project: true,
        tsconfigRootDir: __dirname, // or import.meta.dirname for ESM
      },
    },

    // This is needed in order to specify the desired behavior for its rules
    plugins: {
      '@typescript-eslint': tsPlugin,
    },

    // After defining the plugin, you can use the rules like this
    rules: {
      '@typescript-eslint/no-unused-vars': 'error',
    }
  }
]

ESLint legacy config (version <= 8)

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
        // or `project: true` in typescript-eslint version >= 5.52.0
      },
    },
  ],
  // ...
}

You can set project: true since typescript-eslint 5.52.0.

You can read more about the overrides config on the official ESLint 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, or in version >= 9 you can use globals.

Upvotes: 345

Ali80
Ali80

Reputation: 8646

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

If you don't care if the file is compiled (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

Upvotes: 8

user291466
user291466

Reputation: 21

adding to .eslintrc.js works fine:

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

Upvotes: 0

monotype
monotype

Reputation: 464

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'],
    },
  ],

Upvotes: 1

Wenfang Du
Wenfang Du

Reputation: 11337

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',
  },
}

Upvotes: 7

Zyncho
Zyncho

Reputation: 447

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.

Upvotes: 0

three
three

Reputation: 8478

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.

Upvotes: 0

Caleb Jay
Caleb Jay

Reputation: 2199

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.

Upvotes: 2

John Szafraniec
John Szafraniec

Reputation: 21

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 :)

Upvotes: 1

Hossein Mousavi
Hossein Mousavi

Reputation: 3824

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"],

Upvotes: 1

preet kaur
preet kaur

Reputation: 11

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.

Upvotes: 1

Sherif eldeeb
Sherif eldeeb

Reputation: 2186

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": [""]
}

Upvotes: 1

shamaseen
shamaseen

Reputation: 2488

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

enter image description here

I cleared that and everything worked :\

Upvotes: 0

magentaqin
magentaqin

Reputation: 2139

Add one line in ".eslintignore":

.eslintrc.js

Upvotes: 92

adi1ya
adi1ya

Reputation: 402

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

ignorePatterns: ['.eslintrc.js']

Upvotes: 12

Saurab Bajgain
Saurab Bajgain

Reputation: 1

Set parserOptions in .eslintrc as below

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

Upvotes: -4

Wonder Wonder
Wonder Wonder

Reputation: 187

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'"

Upvotes: 2

user9027325
user9027325

Reputation: 145

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.

Upvotes: 1

jony89
jony89

Reputation: 5367

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.

Upvotes: 4

Alisson Leal
Alisson Leal

Reputation: 194

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🤦

Upvotes: 0

Waldemar Lehner
Waldemar Lehner

Reputation: 1065

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.

Upvotes: 4

Sebastian Kropp
Sebastian Kropp

Reputation: 376

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.

Upvotes: 15

GorvGoyl
GorvGoyl

Reputation: 49220

  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!

Upvotes: 23

codejockie
codejockie

Reputation: 10864

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.

Upvotes: 32

dankoiDev
dankoiDev

Reputation: 123

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.

Upvotes: 1

Freewalker
Freewalker

Reputation: 7315

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.

Upvotes: 1

btd1337
btd1337

Reputation: 2994

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.

Upvotes: 3

Željko Šević
Željko Šević

Reputation: 4354

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

Upvotes: 118

Related Questions