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

新手上路,请多包涵

我刚刚使用 create-react-app 创建了一个新模板,其中包含 react 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"
  }
}

这是我的 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"
  }
}

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

 Line 113:13:  'isLoading' is assigned a value but never used  no-unused-vars

我从事过以前的项目,并且代码中显示了 eslint 错误,而不会导致应用程序崩溃。谁能指出我搞砸的地方?

原文由 ali hussein 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.7k
2 个回答

当我使用 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 钩子时,当你尝试提交带有 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. 安装依赖项并启动应用程序。

原文由 Ashfaq nisar 发布,翻译遵循 CC BY-SA 4.0 许可协议

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

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

为了让您的代码在开发过程中保持 lint 但不失败,请将其添加到您的 .env.development 中ESLINT_NO_DEV_ERRORS=true

要在生产构建中完全禁用 eslint,请将其添加到您的 .env.production 中DISABLE_ESLINT_PLUGIN=true

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

原文由 Thales Kenne 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进