Cannot find module 'eslint-plugin-eslint' error - eslint

I'm setting up a new project with React in Javascript environment.
I creatd a project through CRA(version 18.2.0), and I am configuring ESLint and Prettier environments.
However, when I ran npm run start after setting the configuration, the error occurred.
What could be the workaround?
Failed to load plugin 'eslint' declared in '.eslintrc.js': Cannot find module 'eslint-plugin-eslint'
And this is my ESLint setup.
//.eslintrc.js
module.exports = {
env: {
browser: true,
es2020: true,
},
extends: [
"plugin:import/errors",
"plugin:import/warnings",
"plugin:prettier/recommended",
"plugin:eslint/recommended",
"plugin:react/recommended", // Uses the recommended rules from #eslint-plugin-react
"prettier",
],
parserOptions: {
ecmaVersion: "latest", // Allows for the parsing of modern ECMAScript features
sourceType: "module", // Allows for the use of imports
ecmaFeatures: {
jsx: true, // Allows for the parsing of JSX
},
},
rules: {
"react/prop-types": "off",
"no-extra-semi": "error",
"react/jsx-filename-extension": [1, { extensions: ["js", "jsx"] }],
"arrow-parens": ["warn", "as-needed"],
"no-unused-vars": "warn",
"no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
"no-debugger": process.env.NODE_ENV === "prod" ? "warn" : "off",
"import/prefer-default-export": ["off"],
"react-hooks/exhaustive-deps": ["warn"],
"react/jsx-props-no-spreading": [1, { custom: "ignore" }],
"react/jsx-key": "warn",
"react/react-in-jsx-scope": "off",
"import/extensions": [
"error",
"ignorePackages",
{
"js": "never",
"jsx": "never"
}
],
},
}
//package.json
"devDependencies": {
"eslint": "^8.2.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.28.0",
"eslint-plugin-react-hooks": "^4.3.0"
}
how can i solve it?

if you did not eject your CRA, you do not need to install eslint yourself as it is in dependency of react-script. Pls check how to extend default eslint config here:
https://create-react-app.dev/docs/setting-up-your-editor/#extending-or-replacing-the-default-eslint-config

Related

This experimental syntax requires enabling the parser plugin: 'classPrivateMethods'

I am getting this error. There are a few similar cases that I have seen in Stackoverflow, but I checked and tried everything and the solutions didn't work.
The project itself works, it is just when I try to use pkg, I am getting the error:
> pkg src/index.js --target=latest-win-x64 --out-path ./dist --config pkg.json
:
:
> Error! This experimental syntax requires enabling the parser plugin: 'classPrivateMethods' (441:2)
E:\Proj\node_modules\node-webpmux\webp.js
All solutions basically say to add plugin-proposal-class-properties and plugin-proposal-private-methods, and specify them in the project json files (eslint, babel, package). I did that, and it doesn't help.
package.json:
"devDependencies": {
"#babel/core": "^7.14.8",
"#babel/eslint-parser": "^7.14.7",
"#babel/plugin-proposal-class-properties": "^7.14.5",
"#babel/plugin-proposal-private-methods": "^7.14.5",
"#babel/preset-env": "^7.14.8",
"#babel/preset-react": "^7.14.5",
"eslint": "^7.31.0"
},
.eslintrc.json:
{
"extends": "eslint:recommended",
"parser": "#babel/eslint-parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module",
"babelOptions": {
"plugins": ["#babel/plugin-proposal-class-properties",
"#babel/plugin-proposal-private-methods"]
}
},
"plugins": [
"classPrivateMethods",
"classPrivateProperties",
"babel"
],
"env": {
"node": true
},
"rules": {
"no-console": 0,
"no-unused-vars": 1,
"babel/semi": 1
}
}
.babelrc:
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
[
"#babel/plugin-proposal-class-properties"
]
]
}
.pkg.json:
{
"name": "WA chatbot",
"version": "1.0.0",
"pkg": {
"assets": "./assets"
}
}
node version: v14.15.3
yarn: v1.22.10
Is there a solution for this?

How can I get Prettier to Let Me Use Tabs

I'm using ESLint and Prettier with the AirBNB style guide.
For the life of me I can't get it to stop yelling at me on every single line of my file to use spaces.
.eslintrc.json
{
"env": {
"browser": true,
"es2021": true
},
"extends": ["airbnb-base", "prettier", "plugin:node/recommended"],
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error",
"indent": ["warn", "tab"],
"quotes": ["warn", "single"],
"no-unused-vars": "warn",
"no-console": "off",
"no-tabs": "off",
"no-restricted-syntax": "off",
"func-names": "off"
}
}
package.json dependencies
"devDependencies": {
"autoprefixer": "^10.1.0",
"clean-css-cli": "^4.3.0",
"concurrently": "^5.3.0",
"cross-env": "^7.0.3",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.23.4",
"live-server": "^1.2.1",
"postcss-cli": "^8.3.1",
"postcss-import": "^14.0.2",
"pwa-asset-generator": "^4.1.1",
"tailwindcss": "^2.0.2"
}
settings.json
{
"editor.linkedEditing": true,
"editor.detectIndentation": true,
"notebook.cellToolbarLocation": {
"default": "right",
"jupyter-notebook": "left"
},
"prettier.tabWidth": 4,
"prettier.useTabs": true,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"eslint.alwaysShowStatus": true,
"workbench.colorTheme": "Default Dark+",
"prettier.singleQuote": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
I have tabs set everywhere. This exact setup worked before (I had to factory reset my machine). I have my VS code settings synced, and the eslintrc and pacakge.json files should be the exact same as before...
Restarting VS code a second time worked...

eslint import/order react typescript

I want to configure eslint import/order to a custom configuration, but I cannot make it work in react typescript files.
Here is my rule:
"import/order":[
"error",
{
"groups": ["builtin", "external", "internal"],
"pathGroups":[{
"pattern": "react",
"group": "external",
"position": "before"
}],
"pathGroupsExcludedImportTypes": ["react"],
"newlines-between": "always",
"alphabetize": {
"order": "asc",
"caseInsensitive":true
}
}
]
.eslintrc.json file:
{
"parser": "#typescript-eslint/parser",
"parserOptions": {
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 6,
"project": "./tsconfig.json"
},
"env": {
"browser": true,
"jest": true,
"es6": true
},
"extends": [
"airbnb-typescript",
"airbnb/hooks",
"prettier",
"prettier/react",
"plugin:#typescript-eslint/recommended",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"plugin:jest/recommended",
"prettier/#typescript-eslint",
"plugin:prettier/recommended"
],
"plugins": [
"react",
"react-hooks",
"import",
"#typescript-eslint",
"prettier",
"jest"
],
"rules": {
"no-console": 2,
"prettier/prettier": [
"error",
{
"singleQuote": true
}
],
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "off",
"react/jsx-filename-extension": [
"off"
],
"react/jsx-closing-bracket-location": [
1,
"tag-aligned"
],
"import/no-extraneous-dependencies": 1,
"import/order":[
"error",
{
"groups": ["builtin", "external", "internal"],
"pathGroups":[{
"pattern": "react",
"group": "external",
"position": "before"
}],
"pathGroupsExcludedImportTypes": ["react"],
"newlines-between": "always",
"alphabetize": {
"order": "asc",
"caseInsensitive":true
}
}
]
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".ts", ".tsx"],
"moduleDirectory": [
"node_modules",
"src/"
]
},
"typescript": {
"alwaysTryTypes": true
}
},
"react": {
"version": "detect"
}
}
}
Current package.json dependencies
"#typescript-eslint/eslint-plugin": "^4.9.1",
"#typescript-eslint/parser": "^4.9.1",
"eslint": "7.2.0",
"eslint-config-airbnb": "18.2.1",
"eslint-config-airbnb-typescript": "^12.0.0",
"eslint-config-prettier": "^7.0.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.1.3",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "^3.2.0",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "4.0.0",
Also in tsconfig.json
"baseUrl": "src",
The rule works fine in .ts files:
rule-working-ts
But it does not work with .tsx files.
Fixed by adding this dependency
yarn add -D eslint-import-resolver-typescript

eslint-plugin-testing-library is not catching lint errors

I'm trying to add eslint-plugin-testing library to a project to catch common errors in our #testing-library/react tests. I've followed the instruction steps, yet I cannot get it to catch errors in the test files.
For example, I manually turn on the no-debug rule, add a debug() statement in a .test.tsx file, and run the linter. It does not catch any mistakes in the file.
If I break rules from other plugins, they are caught, so I suspect I may have something wrong in how I added the testing-library plugin to my config.
package.json
{
"dependencies": {
"react": "16.12.0",
"react-dom": "16.12.0"
},
"devDependencies": {
"#babel/core": "7.7.0",
"#babel/preset-react": "7.0.0",
"#babel/preset-typescript": "7.1.0",
"#testing-library/react": "9.1.3",
"#typescript-eslint/eslint-plugin": "2.15.0",
"#typescript-eslint/parser": "2.15.0",
"eslint": "6.8.0",
"eslint-plugin-cypress": "2.10.3",
"eslint-plugin-jsx-a11y": "6.2.3",
"eslint-plugin-react": "7.19.0",
"eslint-plugin-react-hooks": "2.3.0",
"eslint-plugin-testing-library": "3.0.0",
"typescript": "3.7.3"
}
}
.eslintrc
{
"parser": "#typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"modules": true
}
},
"env": {
"browser": true,
"es6": true,
"jasmine": true,
"jest": true,
"jquery": true,
"node": true
},
"plugins": [
"#typescript-eslint",
"jsx-a11y",
"react-hooks",
"react",
"testing-library"
],
"extends": [
"eslint:recommended",
"plugin:jsx-a11y/recommended",
"plugin:react/recommended",
"plugin:testing-library/recommended"
],
"rules": {
"#typescript-eslint/no-unused-vars-experimental": "off",
"no-unused-vars": "off",
"react-hooks/exhaustive-deps": "warn",
"react-hooks/rules-of-hooks": "error",
"react/display-name": "off",
"react/jsx-uses-react": "error",
"react/jsx-uses-vars": "error",
"react/prop-types": "off",
"testing-library/no-await-sync-query": "error",
"testing-library/no-debug": "error"
},
"settings": {
"react": {
"version": "detect"
}
}
}
Couple of things I've seen in your setup:
you are using "plugin:testing-library/recommended" preset, but the best one for React is "plugin:testing-library/react" as it will enable recommended + react ones. That will enable all the rules with "React" badge under configuration columns
after you change this, you can remove both rules you manually added in your eslint config as they are automatically enabled by react preset
I'm not sure what you mean by "If I break rules from other plugins, they are caught". Let me know if enabling react preset fix this for you. If not, it would be nice to have a small repo to reproduce the error.

Typescript eslint - Missing file extension "ts" import/extensions

I have a simple Node/Express app made with Typescript.
And eslint give me the error
Missing file extension "ts" for "./lib/env" import/extensions
Here is my .eslintrc file
{
"extends": [
"airbnb",
"plugin:#typescript-eslint/recommended",
"prettier",
"prettier/react",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript"
],
"parser": "#typescript-eslint/parser",
"plugins": ["#typescript-eslint", "prettier", "import"],
"settings": {
"import/extensions": [".js", ".jsx", ".ts", ".tsx"],
"import/parsers": {
"#typescript-eslint/parser": [".ts", ".tsx"]
},
"import/resolver": {
"typescript": {
"directory": "./tsconfig.json"
},
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
},
"rules": {
"#typescript-eslint/indent": [2, 2],
"no-console": "off",
"import/no-unresolved": [2, { "commonjs": true, "amd": true }],
"import/named": 2,
"import/namespace": 2,
"import/default": 2,
"import/export": 2
}
}
I have installed eslint-plugin-import & eslint-import-resolver-typescript.
And I cannot figure out why, I got that error.
Add the following code to rules:
"rules": {
"import/extensions": [
"error",
"ignorePackages",
{
"js": "never",
"jsx": "never",
"ts": "never",
"tsx": "never"
}
]
}
airbnb ESLint config leads the problem.
I know this is late, but if you're extending the Airbnb rules you can use eslint-config-airbnb-typescript. Please refer to the project page for the latest instructions. Here's the gist of it as of March 2022:
Install
npm install -D eslint-config-airbnb-typescript
ESLint config file
using React - add 'airbnb-typescript' after 'airbnb'
extends: [
'airbnb',
+ 'airbnb-typescript'
]
without React - add 'airbnb-typescript/base' after 'airbnb-base'
extends: [
'airbnb-base',
+ 'airbnb-typescript/base'
]
set parserOptions.project to path of your tsconfig.json
{
extends: ['airbnb', 'airbnb-typescript'],
+ parserOptions: {
+ project: './tsconfig.json'
+ }
}
NOTE: Prior to August 2021, you would need to also do the following tasks.
These instructions are remaining here for historical reference only. You should not need to do this any more.
First uninstall the old one and add the new one:
# uninstall whichever one you're using
npm uninstall eslint-config-airbnb
npm uninstall eslint-config-airbnb-base
# install the typescript one
npm install -D eslint-config-airbnb-typescript
Then update your ESlint config, remove the old Airbnb from the "extends" section and add the new one:
extends: ["airbnb-typescript"]
This is a strange solution, but I've added "": "never" and it worked for me (https://github.com/import-js/eslint-plugin-import/issues/1573).
"rules": {
"import/extensions": [
"error",
"ignorePackages",
{
"": "never",
"js": "never",
"jsx": "never",
"ts": "never",
"tsx": "never"
}
]
}
Important to add import extension and parser to .eslintrc
"settings": {
"import/extensions": [".js", ".jsx", ".ts", ".tsx"],
"import/parsers": {
"#typescript-eslint/parser": [".ts", ".tsx"]
},
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
}
},
Further add to rules:
"import/extensions": [
"error",
"ignorePackages",
{
"js": "never",
"jsx": "never",
"ts": "never",
"tsx": "never"
}
]
enter code here
got this working by bundling all answers I found on internet:
this is the end result for me:
{
"settings": {
"import/extensions": [".js", ".jsx", ".ts", ".tsx"],
"import/parsers": {
"#typescript-eslint/parser": [".ts", ".tsx"]
},
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
},
"env": {
"browser": true,
"es2021": true
},
"extends": ["plugin:react/recommended", "airbnb"],
"parser": "#typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": ["import", "react", "#typescript-eslint"],
"rules": {
"no-console": 1,
"import/extensions": [
"error",
"ignorePackages",
{
"js": "never",
"jsx": "never",
"ts": "never",
"tsx": "never",
"mjs": "never"
}
]
}
}
im using react native btw. if u're using something different just removing react related should be enough
If you're like me trying to configure babel, legacy Javascript, and new typescript files and attempting to enable extension free imports on *.ts then I was running into this ESLint issue (where I also found below solution):
In addition to the rules config:
"rules": {
"import/extensions": [
"error",
"ignorePackages",
{
"js": "never",
"mjs": "never",
"jsx": "never",
"ts": "never",
"tsx": "never"
}
]
}
You also need an additional settings entry in your .eslintrc.js:
"settings": {
"import/extensions": [".js", ".mjs", ".jsx", ".ts", ".tsx"]
},
Good luck!
Following is a similar error that can be thrown due to '~' in imports:
Missing file extension for "~/path/"
eslintimport/extensions\
Solution 1:
This issue can be resolved by configuring tsconfig.json and .eslintrc.js as follows.
tsconfig.json
...
“paths”: {
“~*”: [“./src/*”],
},
...
.eslintrc.js
...
settings: {
'import/resolver': {
alias: {
map: [['~', './src/']],
extensions: ['.ts', '.js', '.tsx'],
},
},
},
...
Solution 2:
If the error still persists, then probably you must have opened the project in another directory other than the root directory (the directory where tsconfig.json and .eslintrc.js are located). Therefore opening the directory other than the root directory can confuse the eslint in identifying the path correctly, and throw the above error.
In my case, I was extending monorepo ESLint configs, and had airbnb-base loading after the package in which I was disabling that rule. So I just had to load it last.

Resources