【问题标题】:Different ESLint results when building React app构建 React 应用程序时不同的 ESLint 结果
【发布时间】:2021-12-31 11:12:36
【问题描述】:

我是 React 开发的初学者。
我正在尝试解决我的项目的所有错误/警告,但我在开发环境和生产环境之间得到不同的结果。我在配置中没有对它们进行任何区分。

运行 npm run lint 给我这个输出: npm run lint

运行 npm run build 给我这个输出: npm run build

我得到不同的 ESLint 输出是否正常?

这是我的 package.json:

{
  "name": "immersion-dashboard",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@reduxjs/toolkit": "^1.5.1",
    "@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/node": "^12.0.0",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.9.0",
    "@types/react-redux": "^7.1.7",
    "bootstrap": "^5.1.3",
    "react": "^17.0.2",
    "react-bootstrap": "^2.0.2",
    "react-dom": "^17.0.2",
    "react-redux": "^7.2.0",
    "react-scripts": "4.0.3",
    "typescript": "~4.1.5"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.4.0",
    "@typescript-eslint/parser": "^5.4.0",
    "cross-env": "^7.0.3",
    "eslint": "^7.32.0",
    "eslint-config-airbnb": "^19.0.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-import-resolver-typescript": "^2.5.0",
    "eslint-plugin-import": "^2.25.3",
    "eslint-plugin-jest": "^25.2.4",
    "eslint-plugin-jsx-a11y": "^6.5.1",
    "eslint-plugin-react": "^7.27.0",
    "eslint-plugin-react-hooks": "^4.3.0",
    "husky": "^7.0.4",
    "lint-staged": "^12.0.2",
    "prettier": "^2.4.1"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "test:staged": "cross-env CI=true react-scripts test --env=jsdom --passWithNoTests",
    "eject": "react-scripts eject",
    "typescript": "tsc --project tsconfig.json --noEmit",
    "prettier": "prettier . --write",
    "lint": "eslint . --ext .ts --ext .tsx --fix",
    "lint-staged": "lint-staged"
  },
  "lint-staged": {
    "*.{ts,tsx,json,css}": "prettier --write",
    "*.{ts,tsx}": [
      "eslint --fix",
      "npm run test:staged"
    ]
  }
}

还有我的 .eslintrc.json:

{
  "env": {
    "browser": true,
    "es2021": true,
    "jest/globals": true
  },
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "airbnb",
    "prettier"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 12,
    "sourceType": "module"
  },
  "plugins": ["react", "@typescript-eslint", "jest"],
  "rules": {
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "ts": "never",
        "tsx": "never"
      }
    ],
    "react/jsx-filename-extension": [
      "warn",
      {
        "extensions": [".tsx"]
      }
    ],
    "no-use-before-define": "off",
    "@typescript-eslint/no-use-before-define": ["error"],
    "no-param-reassign": [
      "error",
      {
        "props": false
      }
    ],
    "no-console": "off",
    "@typescript-eslint/no-unused-vars": [
      "error",
      {
        "argsIgnorePattern": "^_",
        "varsIgnorePattern": "^_"
      }
    ]
  },
  "settings": {
    "import/resolver": {
      "typescript": {}
    }
  }
}

【问题讨论】:

    标签: reactjs typescript eslint package.json eslintrc


    【解决方案1】:

    恐怕您正在运行两个具有不同配置的 ESLint 实例,让我解释一下原因。

    创建 React 应用 ESLint

    CRA 已经为您设置了 ESLint(除其他外):

    在后台,我们使用 webpack、Babel、ESLint 和其他令人惊叹的项目来为您的应用程序提供动力。

    在 CRA 中,当您运行 startbuild 命令时,ESLint 已安装并在内部运行,因此您在开发应用程序或构建最终包时会在控制台中看到输出。是ou can also get the lint output in your editor。 CRA 已经包含 ESLint 的默认配置,包括几个插件。

    你的项目 ESLint

    您已在项目中单独安装 ESLint,并在您自己的 .eslintrc.json 中设置它。那绝对没问题!这是 lint 项目的常用方法。运行此 ESLint 的方式是通过添加到脚本中的 lint 命令。


    因此,当您运行 startbuild 时,您正在使用 CRA 的 ESLint 实例和配置对您的项目进行 linting,但是当您运行 lint 时,您正在使用您的 ESLint 实例和配置对您的项目进行 linting。他们的配置不匹配 100%,因此报告了不同的错误。

    您可以通过运行npm ls eslint 来检查您是否安装了两个 ESLint 实例,您会看到如下内容:

    在那里您可以看到一个直接的 eslint 依赖项(您手动安装的那个),以及另一个属于 react-scriptseslint(CRA 作为子依赖项安装的那个)。

    你怎么能解决这个问题?

    基本上你有两个选择:

    1. 删除您的 ESLint 并自定义 CRA ESLint。您可以从项目中卸载您的 eslint 依赖项,删除您的自定义 .eslintrc.jsonextend CRA ESLint config。这必须通过package.json 中的eslintConfig 键来完成。你不需要把你在.eslintrc.json 中的所有东西都放在那里,因为其中大部分已经被 CRA 配置覆盖了。此选项的缺点是 1)您不能使用 npm run lint 按需对代码进行 lint,因为 CRA 不允许您这样做,并且 2)您与 CRA 使用的 ESLint 插件版本相关联(例如他们正在使用eslint-plugin-testing-library v3,但最新的是v5,你不能使用它)。
    2. 忽略 CRA 的 ESLint(推荐)。这是我通常做的。 You can opt-out of the CRA built-in ESLint instance。为此,您需要在项目的根目录中创建一个.env 文件,然后将DISABLE_ESLINT_PLUGIN=true 放入其中。之后,CRA 不会在运行 startbuild 时对您的代码进行 lint,因此何时使用 lint 命令对其进行 lint 取决于您。理想情况下,您将在 CI 中运行 lint 命令,并在每次使用 lint-staged 提交文件时在本地运行(这对您来说可能并不熟悉,如果您需要帮助来设置这些命令,请告诉我),除了通过代码编辑器获得 ESLint 错误的即时反馈之外(设置 VSCode 或 WebStorm 应该非常简单)。

    希望对您有所帮助,如果您还有其他想讨论的,请告诉我!

    【讨论】:

    • 感谢您的完整回答!我已经用 husky 设置了 lint-staged 以进行预提交,所以我现在准备开始开发,但再次感谢您的帮助!
    • 我很高兴它有帮助!
    猜你喜欢
    • 2016-03-12
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 2016-06-11
    • 2020-04-03
    • 1970-01-01
    • 2022-09-28
    相关资源
    最近更新 更多