简体   繁体   English

Eslint 错误导致 create-react-app 编译失败

[英]Eslint error causing create-react-app failed to compile

I am building a website using create-react-app and have just installed eslint to it.我正在使用create-react-app构建一个网站,并且刚刚为其安装了eslint

For some reason what was supposed to be shown as eslint warnings are showing up as errors and causing npm run start to fail.由于某种原因,应该显示为 eslint 警告的内容显示为错误并导致npm run start失败。

How can I bypass this issue and have them shown as warnings like before?我怎样才能绕过这个问题并像以前一样将它们显示为警告?

My.eslintrc.js我的.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'],
};

My package.json我的 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

sorry.对不起。 I am not allowed to comment but I really want to help you.我不能发表评论,但我真的很想帮助你。

I assume that you have installed eslint using npm install eslint --save-dev and defined a default configuration with node_modules/.bin/eslint --init answering the questions in the prompt.我假设您已经使用npm install eslint --save-dev并使用node_modules/.bin/eslint --init回答提示中的问题定义了默认配置。

I notice that in your .eslintrc.js file, the eslint settings is missing in the extends option:我注意到在您的.eslintrc.js文件中,extends 选项中缺少 eslint 设置:

extends: [
    'eslint:recommended',
    'airbnb-typescript',
    'plugin:@typescript-eslint/recommended',
    'prettier/react',
    'prettier/@typescript-eslint',
    'plugin:prettier/recommended',
  ],

Also in the package.json is recommended eslint to have its own script that you run using npm run lint and use it combined with a eslint-plugin in your favorite code editor:同样在package.json中,建议 eslint 拥有自己的脚本,您可以使用npm run lint运行该脚本,并将其与您最喜欢的代码编辑器中的 eslint-plugin 结合使用:

{
  "scripts": {
    "start": "react-scripts start",
    // ...
    "lint": "eslint ."
  },
}

Probably you will build you application in some moment, so you should create a .eslintignore file and inside of it add build since files in the build directory get checked when the command is run.可能您将在某个时刻构建应用程序,因此您应该创建一个.eslintignore文件并在其中添加build因为在运行命令时会检查构建目录中的文件。

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

This part in your package.json is unnecessary;你的package.json这部分是不需要的; since you have an aslant config file, it should be moved to .eslintrc.js .因为你有一个 aslant 配置文件,它应该被移动到.eslintrc.js

"eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
}

Which would then turn into this:然后会变成这样:

extends: [
    'react-app',
    'react-app/jest',
    'airbnb-typescript',
    'plugin:@typescript-eslint/recommended',
    'prettier/react',
    'prettier/@typescript-eslint',
    'plugin:prettier/recommended'
]

However;然而; in the latest versions of the eslint-config-react-app plugin at this time (7.31.11) , I'm getting a jest plugin conflict error with a project that worked prior to updating unless I remove react-app/jest from my eslint configs extends section.在此时(7.31.11)的最新版本的eslint-config-react-app插件中,除非我从我的项目中删除react-app/jest eslint 配置extends部分。

Which is how I ended up here, currently trying to find out what caused this.这就是我最终来到这里的原因,目前正试图找出造成这种情况的原因。

Update: So my issue was caused because eslint-config-react-app depends on eslint-plugin-jest^25.7.0 and I was using the latest eslint-plugin-jest^27.1.6 .更新:所以我的问题是因为eslint-config-react-app依赖于eslint-plugin-jest^25.7.0而我使用的是最新的eslint-plugin-jest^27.1.6 I removed my package.json 's dependency and will use the version included with eslint-config-react-app so there aren't any conflicts there, but if I need features of the newer plugin version, a quick npm i -D on it, changing eslint configuration from automatic to manual and specifying the path to the local node_modules version and .eslintrc.js should work as well;我删除了我的package.json的依赖项,并将使用eslint-config-react-app中包含的版本,因此那里没有任何冲突,但如果我需要更新插件版本的功能,快速npm i -D就可以了,将 eslint 配置从自动更改为手动,并指定本地node_modules版本和.eslintrc.js的路径也应该有效; aside from any conflicts with the config plugin.除了与配置插件的任何冲突。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM