I actually had this problem the other day; you need to go to your .eslintrc
and make sure that the module is there under the parser property of the config. Should look something like this in the end:
{
//...
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
],
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
//...
}
This should cover the essentials regarding dependencies in your linter. Hope it helps.
Edit
I checked GitHub for this issue, might not be the same as the one I had, check this link please.
Answer from bloo on Stack Overflow
» npm install typescript-eslint-parser
» npm install typescript-eslint
I actually had this problem the other day; you need to go to your .eslintrc
and make sure that the module is there under the parser property of the config. Should look something like this in the end:
{
//...
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
],
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
//...
}
This should cover the essentials regarding dependencies in your linter. Hope it helps.
Edit
I checked GitHub for this issue, might not be the same as the one I had, check this link please.
Had this problem also, I resolved disabling, then reloading and re-enabling ESLint extension of VSCode.
Hope this helps ;)
Different lint rules for JavaScript and TypeScript files
The problem happens for one of the reasons below:
- You're using a rule which require type information and didn't specify
a
parserOptions.project; - You specified
parserOptions.project, didn't specifycreateDefaultProgram(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.
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
» npm install typescript-eslint-parser-for-extra-files
Explanation
If you create a new project with npm init -y and then npm install @typescript-eslint/eslint-plugin, you would see in your package-lock.json:
"node_modules/@typescript-eslint/eslint-plugin": {
"version": "6.2.1",
...
"peerDependencies": {
"@typescript-eslint/parser": "^6.0.0 || ^6.0.0-alpha",
...
}
...
}
This means @typescript-eslint/eslint-plugin version 6.2.1 needs @typescript-eslint/parser version ^6.0.0 to be in your project.
Meanwhile, here is what you have in your current package-lock.json:
"node_modules/eslint-config-next": {
"version": "13.4.8",
...
"dependencies": {
...
"@typescript-eslint/parser": "^5.42.0",
...
},
...
},
Meaning eslint-config-next needs version 5.42.0, so while installing @typescript-eslint/eslint-plugin, npm founds a versions conflict.
Solution
You can install @typescript-eslint/eslint-plugin version 5.9.0, which expects the version ^5.0.0 of @typescript-eslint/parser :
npm i @typescript-eslint/[email protected]
I got a similar issue working with a create-react-app which I was converting to use typescript. As Youssouf suggested, the issue is the conflict of versions between @typescript-eslint and some other sub-modules, such as @typescript-eslint/parser. This was evident by my package-lock.json listing several @typescript-eslint sub-modules as at version 5.6.2.
I fixed this by first running npm i @typescript-eslint/parser@latest to upgrade the sub-modules in my package-lock. I was then able to run npm i @typescript-eslint/eslint-plugin successfully.