Eslint错误导致create-react-app无法编译

lia*_*iam 7 reactjs eslint eslintrc typescript-eslint

我正在使用它构建一个网站create-react-app,并且刚刚安装了eslint它。

由于某种原因,本应显示为 eslint 警告的内容却显示为错误并导致npm run start失败。

我怎样才能绕过这个问题并像以前一样将它们显示为警告?

我的 .eslintrc.js

  env: {
    browser: true,
    es6: true,
    jest: true,
  },
  extends: [
    'airbnb-typescript',
    'plugin:@typescript-eslint/recommended',
    'prettier/react',
    'prettier/@typescript-eslint',
    'plugin:prettier/recommended',
  ],
  globals: {
    Atomics: 'readonly',
    SharedArrayBuffer: 'readonly',
  },
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 2018,
    sourceType: 'module',
    project: './tsconfig.json',
  },
  plugins: ['react', '@typescript-eslint'],
  rules: {
    'class-methods-use-this': 'off',
    'additional-rule': 'warn',
  },
  ignorePatterns: ['**/node_modules/*', '**/build/*', 'config-overrides.js'],
};
Run Code Online (Sandbox Code Playgroud)

我的package.json

  "name": "device-protection-renewal-web",
  "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",
    "@types/jest": "^26.0.15",
    "@types/node": "^12.19.3",
    "@types/react": "^16.9.55",
    "@types/react-dom": "^16.9.9",
    "babel-polyfill": "^6.26.0",
    "core-js": "^3.6.5",
    "i18next": "^19.8.3",
    "react": "^17.0.1",
    "react-app-polyfill": "^2.0.0",
    "react-dom": "^17.0.1",
    "react-i18next": "^11.7.3",
    "react-scripts": "4.0.0",
    "web-vitals": "^0.2.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all",
      "ie >= 9"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version",
      "ie >= 9"
    ]
  },
  "devDependencies": {
    "@babel/plugin-transform-arrow-functions": "^7.12.1",
    "@typescript-eslint/eslint-plugin": "^4.6.1",
    "@typescript-eslint/parser": "^4.6.1",
    "eslint": "^7.11.0",
    "eslint-config-airbnb-typescript": "^9.0.0",
    "eslint-config-prettier": "^6.11.0",
    "eslint-plugin-import": "^2.22.0",
    "eslint-plugin-jsx-a11y": "^6.3.1",
    "eslint-plugin-prettier": "^3.1.4",
    "eslint-plugin-react": "^7.20.6",
    "eslint-plugin-react-hooks": "^4.1.0",
    "prettier": "^2.1.2",
    "typescript": "^4.0.5"
  }
}```


  [1]: https://i.stack.imgur.com/WUKcz.png
Run Code Online (Sandbox Code Playgroud)

Vin*_*cio 5

我假设您已经使用 ESLint 安装npm install eslint --save-dev并定义了默认配置并node_modules/.bin/eslint --init回答了提示中的问题。

我注意到在您的.eslintrc.js文件中,扩展选项中缺少 ESLint 设置:

extends: [
    'eslint:recommended',
    'airbnb-typescript',
    'plugin:@typescript-eslint/recommended',
    'prettier/react',
    'prettier/@typescript-eslint',
    'plugin:prettier/recommended',
  ],
Run Code Online (Sandbox Code Playgroud)

另外,package.json建议 ESLint 有自己的脚本,您可以使用它来运行npm run lint并在您最喜欢的代码编辑器中将其与 eslint 插件结合使用:

{
  "scripts": {
    "start": "react-scripts start",
    // ...
    "lint": "eslint ."
  },
}
Run Code Online (Sandbox Code Playgroud)

您可能会在某个时候构建应用程序,因此您应该创建一个.eslintignore文件并在其中添加文件build,因为运行命令时默认情况下也会检查构建目录中的文件。

来源: https: //fullstackopen.com/en/part3/validation_and_es_lint#lint

  • 我刚刚将我的“react-scripts”降级到“3.4.3”,现在可以编译了。我想我现在可能会坚持使用这个版本。 (4认同)
  • 再一次问好。我已经尝试了上面的解决方案,它消除了错误“React”在定义之前被使用``,但其余错误仍然存​​在。我想知道这是否可能与我正在使用的 ```react-scripts``` 版本有关。这个仓库使用的是“4.0.0”,而我的另一个仓库使用的是“3.4.3”,具有相同的 eslint 版本,并且编译得很好。 (2认同)
  • 遇到类似的情况,手动安装 eslint 导致编译失败。降级到 3.4.3 就成功了 (2认同)