简体   繁体   English

ESLint 错误 - ESLint 找不到配置“react-app”

[英]ESLint error - ESLint couldn't find the config "react-app"

Me with my team, start a new React Project using the create-react-app bootstrap command.我和我的团队使用 create-react-app bootstrap 命令启动一个新的 React 项目。 We add the eslint section on the project but we stuck with annoying error that we never found it before now.我们在项目中添加了 eslint 部分,但我们遇到了以前从未发现过的恼人错误。 When we launch the command当我们启动命令

 yarn run lint

Here the error:这里的错误: 在此处输入图像描述

Here the package.json:这里是 package.json:

{
  "name": "name of the app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "jwt-decode": "^2.2.0",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-intl": "^3.12.0",
    "react-router": "^5.1.2",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.3.0",
    "redux": "^4.0.5",
    "redux-saga": "^1.1.3",
    "reselect": "^4.0.0",
    "styled-components": "^5.0.1"
  },
  "scripts": {
    "analyze": "react-app-rewired build",
    "build": "react-scripts build",
    "cypress": "./node_modules/.bin/cypress open",
    "eject": "react-scripts eject",
    "lint:write": "eslint --debug src/ --fix",
    "lint": "eslint --debug src/",
    "prettier": "prettier --write src/*.tsx src/*.ts src/**/*.tsx src/**/*.ts src/**/**/*.tsx src/**/**/*.ts src/**/**/**/*.tsx src/**/**/**/*.ts src/**/**/**/**/*.tsx src/**/**/**/**/*.ts",
    "start": "react-scripts start",
    "storybook": "start-storybook -p 6006 -c .storybook",
    "test": "react-scripts test",
    "upgrade:check": "npm-check",
    "upgrade:interactive": "npm-check --update"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "lint-staged": {
    "*.(ts|tsx)": [
      "yarn run prettier"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@cypress/webpack-preprocessor": "^4.1.2",
    "@storybook/addon-actions": "^5.3.12",
    "@storybook/addon-info": "^5.3.12",
    "@storybook/addon-knobs": "^5.3.12",
    "@storybook/addon-links": "^5.3.12",
    "@storybook/addons": "^5.3.12",
    "@storybook/preset-create-react-app": "^1.5.2",
    "@storybook/react": "^5.3.12",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "@types/jest": "^24.0.0",
    "@types/jwt-decode": "^2.2.1",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.9.0",
    "@types/react-redux": "^7.1.7",
    "@types/react-router": "^5.1.4",
    "@types/react-router-dom": "^5.1.3",
    "@types/storybook__addon-knobs": "^5.2.1",
    "@types/styled-components": "^4.4.2",
    "cypress": "^4.0.1",
    "eslint": "^6.8.0",
    "husky": "^4.2.1",
    "lint-staged": "^10.0.7",
    "npm-check": "^5.9.0",
    "prettier": "^1.19.1",
    "react-app-rewire-webpack-bundle-analyzer": "^1.1.0",
    "react-app-rewired": "^2.1.5",
    "react-docgen-typescript-webpack-plugin": "^1.1.0",
    "redux-devtools-extension": "^2.13.8",
    "storybook-addon-jsx": "^7.1.14",
    "ts-loader": "^6.2.1",
    "typescript": "~3.7.2",
    "webpack": "^4.41.6",
    "webpack-bundle-analyzer": "^3.6.0"
  }
} 

We also tried to add create a.eslintrc file and add我们还尝试添加create a.eslintrc文件并添加

{
  "extends": "react-app"
}

But nothing change.但没有任何改变。

react-scripts should already include ESLint and the eslint-config-react-app ESLint config (see docs ) in your Node dependencies. react-scripts应该已经在您的 Node 依赖项中包含 ESLint 和eslint-config-react-app ESLint 配置(请参阅文档)。

You don't need to install it separately in Create React App projects.您不需要在 Create React App 项目中单独安装它。

If ESLint is not able to find the react-app config, there are likely conflicting or duplicate ESLint config dependencies that were installed outside of react-scripts (ie another eslint-config-react-app installation).如果 ESLint 无法找到react-app配置,则可能存在冲突或重复的 ESLint 配置依赖项,这些依赖项安装在react-scripts (即另一个eslint-config-react-app安装)。 You can check by using the npm ls [package] command (ie npm ls eslint-config-react-app ).您可以使用npm ls [package]命令(即npm ls eslint-config-react-app )进行检查。

Probably the best way to approach this is to remove those packages from package.json to allow the configs like eslint-config-react-app in react-scripts to be used as intended (based on comment ) .可能解决此问题的最佳方法是从package.json删除这些包,以允许按预期使用react-scripts eslint-config-react-appeslint-config-react-app (基于注释)。 If the problem persists after that, try removing the node_modules folder and package-lock.json and then reinstalling the dependencies again may work.如果此后问题仍然存在,请尝试删除node_modules文件夹和package-lock.json ,然后再次重新安装依赖项可能会奏效。 Otherwise, upgrading to a newer npm version may make a difference.否则,升级到更新的npm版本可能会有所不同。

Old Answer:旧答案:

Your package.json is likely missing the peer dependency eslint-config-react-app which includes the eslint preset react-app .您的package.json可能缺少包含 eslint 预设react-app的对等依赖项eslint-config-react-app react-app You will need to follow the instructions to install it here https://www.npmjs.com/package/eslint-config-react-app .您需要按照此处的说明安装它https://www.npmjs.com/package/eslint-config-react-app

Note: Even though this is create-react-app which bundles it for react-scripts , your lint script was not able to access react-app .注意:尽管这是create-react-app它捆绑到react-scripts ,但您的lint脚本无法访问react-app


"eslintConfig": {
      "extends": "react-app"   
}

About .eslintrc , you already have the config for ESLint in your package.json so you should be safe to remove .eslintrc .关于.eslintrc ,你的package.json已经有 ESLint 的配置,所以你应该可以安全地删除.eslintrc

The problem here is with the yarn package manager.这里的问题在于yarn包管理器。 Because eslint-config-react-app is not under the node_modules directly and it's inside the react-scripts: node_modules\\react-scripts\\node_modules yarn has not load that correctly (more information here )因为eslint-config-react-app不是直接在node_modules下, node_modules在 react-scripts 内: node_modules\\react-scripts\\node_modules yarn 没有正确加载(更多信息在这里

So, currently, there are 2 options to resolve that issue:因此,目前,有两个选项可以解决该问题:

  1. using npm instead of yarn .使用npm代替yarn
  2. install eslint-config-react-app package in addition to the one we have inside react-scripts .安装eslint-config-react-app包,以及我们在react-scripts

I solved this problem by using npm install instead of yarn .我通过使用npm install而不是yarn解决了这个问题。

Not sure why but I guess it's because yarn install multiple packages at a time while npm installs them one after the other.不知道为什么,但我猜是因为 yarn 一次安装多个包,而 npm 一个接一个安装它们。 So maybe some of them are skipped or not installed properly.所以也许其中一些被跳过或没有正确安装。

.git/hooks 中删除预提交文件解决了我的问题。

SOLVED!解决了!

I solve changing my lint script:我解决了更改 lint 脚本的问题:

Old this:老这个:

"lint": "eslint --debug src/"

New to this:新的:

"lint": "eslint --debug 'src/**/*.{js,jsx,ts,tsx}'",

我正在使用纱线,我通过重新安装 vscode 并将 eslint 降级到 7.32 解决了这个问题

If you don't use React in your project just add this in.eslintrc.js:如果你不在项目中使用 React,只需在 .eslintrc.js 中添加:

{
   root: true,
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ESLint 找不到插件“eslint-plugin-@typescript-eslint” - ESLint couldn't find the plugin "eslint-plugin-@typescript-eslint" eslint 无法加载要扩展的配置“react-app”。 在项目中进行更改时 - eslint Failed to load config "react-app" to extend from. when making changes in project Eslint - 找不到 eslint-config-airbnb - Eslint - eslint-config-airbnb not find 找不到插件“eslint-plugin-promise” - couldn't find the plugin “eslint-plugin-promise” ESLint找不到插件“ eslint-plugin-angular”,但我没有使用Angular - ESLint couldn't find the plugin “eslint-plugin-angular” but i'm not using Angular 在不存在的行反应 ESLint 错误 - React ESLint error at line that doesn't exist 无法加载在 'package.json » eslint-config-react-app#overrides[0]' 中声明的解析器 '@typescript-eslint/parser':找不到模块 'typescript' - Failed to load parser '@typescript-eslint/parser' declared in 'package.json » eslint-config-react-app#overrides[0]': Cannot find module 'typescript' Mac WebStorm错误:无法找到模块'eslint-config-airbnb' - Mac WebStorm Error: Cannot find module 'eslint-config-airbnb' 如何解决,craco: *** Cannot find ESLint loader (eslint-loader)。 *** ANTd 和 React 错误 (2021) - How to solve, craco: *** Cannot find ESLint loader (eslint-loader). *** error with ANTd and React (2021) React 中的 for 循环显示 ESLint 错误 - The for loop in React showing ESLint error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM