由于 ESLint 错误,我的 create-react-app 无法编译

ali*_*ein 48 reactjs eslint

我刚刚创建了一个新的模板,create-react-app反应过来V17在内,并为我所用之前,我安装eslint的依赖,这是我的package.json文件

{
  "name": "gym-nation",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.5",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "axios": "^0.21.0",
    "classnames": "^2.2.6",
    "moment": "^2.29.1",
    "prop-types": "^15.7.2",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-helmet": "^6.1.0",
    "react-intl": "^5.8.6",
    "react-redux": "^7.2.1",
    "react-router": "^5.2.0",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.0",
    "redux": "^4.0.5",
    "redux-thunk": "^2.3.0",
    "web-vitals": "^0.2.4"
  },
  "scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.5.0",
    "@typescript-eslint/parser": "^4.5.0",
    "babel-eslint": "^10.1.0",
    "eslint": "^7.12.0",
    "eslint-config-airbnb": "^18.2.0",
    "eslint-config-prettier": "^6.14.0",
    "eslint-config-react-app": "^6.0.0",
    "eslint-plugin-flowtype": "^5.2.0",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-jest": "^24.1.0",
    "eslint-plugin-jsx-a11y": "^6.3.1",
    "eslint-plugin-prettier": "^3.1.4",
    "eslint-plugin-react": "^7.21.5",
    "eslint-plugin-react-hooks": "^4.2.0",
    "eslint-plugin-testing-library": "^3.9.2",
    "node-sass": "^4.14.1",
    "prettier": "^2.1.2",
    "react-app-rewired": "^2.1.6"
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 eslintrc.json:(注意我还没有添加所有规则)

{
  "env": {
    "browser": true,
    "es2021": true
  },
  "extends": ["react-app", "prettier"],
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 12,
    "sourceType": "module"
  },
  "plugins": ["react", "prettier"],
  "rules": {
    "prettier/prettier": [
      "error",
      {
        "printWidth": 140,
        "singleQuote": true,
        "editor.formatOnSave": true,
        "arrowParens": "always",
        "jsxSingleQuote": true,
        "tabWidth": 2,
        "trailingComma": "none"
      }
    ],
    "no-unused-vars": "error"
  }
}
Run Code Online (Sandbox Code Playgroud)

当我运行该应用程序时,由于此错误将无法编译:

Line 113:13:  'isLoading' is assigned a value but never used  no-unused-vars
Run Code Online (Sandbox Code Playgroud)

我在以前的项目中工作过,代码中显示了 eslint 错误,而没有导致应用程序崩溃。任何人都可以指出我搞砸的地方吗?

谢谢

Ash*_*sar 60

当我使用应用程序创建应用程序create-react-app并为应用程序设置 eslint时,我遇到了完全相同的错误。

eslint 错误显示在浏览器中而不是控制台中

有一次,我调试了所有依赖项。似乎react-scripts是导致我的 lint 错误。

最新版本的react-scripts:"4.0.0"可能有一些破坏性的变化,这可能会导致 eslint 在浏览器中抛出错误。

解决方案:

此问题已在 中修复,react-scipts:"4.0.3"但是,项目中存在的 eslint 错误默认不会转换为警告。您必须创建一个.env 文件,其中应包含一个ESLINT_NO_DEV_ERRORS=true标志。由于此标志,您将收到eslint 错误作为警告不是开发中的错误

在生产过程中以及当它们是任何 git hooks 运行时,此标志会被忽略,当您尝试提交带有eslint 错误的代码时,这反过来会导致错误

更新 2:

活动问题:https : //github.com/facebook/create-react-app/issues/9887

在 react-scripts:4.0.2 发布之前,此问题的解决方法:

  • 安装patch-packagepostinstall-postinstall作为 dev 依赖项。

  • 转到node_modules/react-scripts/config/webpack.config.js并进行以下更改 变化

  • 完成编辑后,运行yarn patch-package react-scripts。这将创建一个补丁文件夹,其中包含依赖补丁

  • 现在,因为我们不想在安装依赖项时每次都这样做。我们将在package.json 文件中添加另一个脚本

    "postinstall":"patch-package".

每次安装依赖项时,上面的脚本都会运行。请记住,您还必须将包文件夹推送到您的存储库。如果您需要更改,也可以在部署应用程序时进行更改。

感谢@fernandaad 提供此解决方法。

更新 1:

不得不降级到react-scripts:3.4.4版本,因为目前没有可用的解决方法。现在,错误是在控制台而不是浏览器中抛出的。

  1. 删除 node_modules 和 package.lock/yarn.lock。
  2. 将 react-scripts 从 4.0.0 降级到 3.4.4。
  3. 安装依赖项并启动应用程序。

  • 我也无法在 v4.0 中找到此问题的解决方案,因此将降级到 v4.0 之前的版本 (3认同)
  • 它看起来像“react-scripts:4.0.0”使用的“eslint 7.0”以某种方式强制执行更严格的策略,对某些插件抛出错误而不是警告。对我来说,它不适用于“eslint-airbnb-base”,几乎阻止了任何编译,即使是一个微小的“no-unused-vars”错误。降级到“3.4.4”似乎是不弹出和自定义 webpack 配置到更深层次的唯一方法 (3认同)
  • 你就是山羊!该环境变量刚刚起作用了! (2认同)
  • React-scripts 5.0 中仍然存在 bug。不得不降级到3.4.4 (2认同)

Has*_*alp 15

对于Next.js

打开next.config.js并启用ignoreDuringBuildseslint 配置中的选项:

module.exports = {
  eslint: {
    ignoreDuringBuilds: true
  }
}
Run Code Online (Sandbox Code Playgroud)


Tha*_*nne 14

我能够使用 env 变量修复它,因为我没有使用 webpack。

确保您使用的是 VSCode 扩展并且您安装了 eslint 作为开发依赖项(以及您可能需要的任何其他插件)。

要让您的代码在开发中 lint,但不会失败,请将其添加到您的.env.developmentESLINT_NO_DEV_ERRORS=true

要在生产版本中完全禁用 eslint,请将其添加到您的.env.productionDISABLE_ESLINT_PLUGIN=true

之后,它不应使任何构建失败。我不明白为什么 eslint 构建失败。它可以帮助我们保持代码质量,但不一定强制执行。

  • 感谢您“DISABLE_ESLINT_PLUGIN=true”!!! (2认同)
  • `ESLINT_NO_DEV_ERRORS=true` 非常适合我继续开发,而不会因 lint 错误而导致编译失败。谢谢 (2认同)

red*_*dia 11

React-scripts 的开发人员决定一旦CI=true出现警告就将其转变为错误。因此,解决这个问题的唯一方法是使用CI=false你的命令或让你的 eslint 运行时没有警告。此处没有必要建议 eslint 规则,因为您的代码库或自定义规则集引起的另一个警告可能会触发该问题。

调整你的scriptspackage.json

"scripts": {
  "start": "CI=false react-app-rewired start",
  "build": "CI=false react-app-rewired build",
  "test": "react-app-rewired test",
  "eject": "react-scripts eject"
}
Run Code Online (Sandbox Code Playgroud)

或者这样运行:

CI=false npm run start
Run Code Online (Sandbox Code Playgroud)
CI=false npm run build
Run Code Online (Sandbox Code Playgroud)

查看 GitHub 上的相关问题:

https://github.com/facebook/create-react-app/issues/10062

https://github.com/facebook/create-react-app/issues/9887


Mas*_*ana 9

这个编译问题也可以通过在 .env 文件中添加它来解决。我面对这个问题并尝试使用这行代码来解决它。

SKIP_PREFLIGHT_CHECK=true

或者

ESLINT_NO_DEV_ERRORS=true

SKIP_PREFLIGHT_CHECK=true or ESLINT_NO_DEV_ERRORS=true
Run Code Online (Sandbox Code Playgroud)

或者您也可以编辑 package.json。

"start": "ESLINT_NO_DEV_ERRORS='true'  react-scripts start",  
"build": "DISABLE_ESLINT_PLUGIN='true' react-scripts build",
Run Code Online (Sandbox Code Playgroud)


Cit*_*ror 8

我花了几个小时才找到可能的解决方案:

1-解决方法:在package.json文件中添加规则

  "eslintConfig": {
    "extends": ["react-app"],
    "rules": {
      "no-unused-vars": "warn"
    },
    "overrides": [
      {
        "files": ["**/*.ts?(x)"],
        "rules": {
          "max-len": "warn"
        }
      }
    ]
  },
Run Code Online (Sandbox Code Playgroud)

2- 您可以使用.eslintrc.js文件,通过在根文件夹中添加具有以下内容的.env文件:

DISABLE_ESLINT_PLUGIN=true
ESLINT_NO_DEV_ERRORS=true
Run Code Online (Sandbox Code Playgroud)
  • DISABLE_ESLINT_PLUGIN- 将在开发和生产中禁用eslint-webpack-plugin。- 这是必须的。

  • ESLINT_NO_DEV_ERRORS - 在开发过程中将 ESLint 错误转换为警告。因此,ESLint 输出将不再出现在错误叠加中。仅此一项并不能修复该错误。

另请参阅此处此处的文档


小智 5

对于临时解决方法,请使用.eslintrc.js以便您可以检查当前的节点环境并相应地设置规则。例如:

const isProd = process.env.NODE_ENV === 'production';
const warnDevErrorProd = isProd ? 2 : 1;

{
  ...
  "rules": {
    ...
    "prettier/prettier": warnDevErrorProd,
    "no-console": warnDevErrorProd,
    ...
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)

这样你就不需要弹出来修改你的 webpack 配置来更改那里的规则。