简体   繁体   English

有没有办法更改弹出的 create-react-app 的 eslint 规则?

[英]Is there a way to change eslint rules for ejected create-react-app?

I am building an app with React and using NPM.我正在使用 React 和 NPM 构建一个应用程序。

I have set up my package.json like so我已经像这样设置了我的 package.json

{
  "name": "mouseflow-react",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/core": "7.7.4",
    "@material-ui/core": "^4.1.3",
    "@material-ui/icons": "^4.1.3",
    "@material-ui/styles": "^4.2.1",
    "@svgr/webpack": "4.3.3",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.4.0",
    "@testing-library/user-event": "^7.1.2",
    "axios": "^0.19.0",
    "babel-eslint": "10.0.3",
    "babel-jest": "^24.9.0",
    "babel-loader": "8.0.6",
    "babel-plugin-named-asset-import": "^0.3.5",
    "babel-preset-react-app": "^9.1.0",
    "camelcase": "^5.3.1",
    "case-sensitive-paths-webpack-plugin": "2.2.0",
    "cookie": "^0.4.0",
    "css-loader": "3.2.0",
    "dotenv": "8.2.0",
    "dotenv-expand": "5.1.0",
    "eslint": "^6.6.0",
    "eslint-config-airbnb": "^18.0.1",
    "eslint-config-react-app": "^5.1.0",
    "eslint-loader": "3.0.2",
    "eslint-plugin-flowtype": "3.13.0",
    "eslint-plugin-import": "2.18.2",
    "eslint-plugin-jsx-a11y": "6.2.3",
    "eslint-plugin-react": "7.16.0",
    "eslint-plugin-react-hooks": "^1.6.1",
    "file-loader": "4.3.0",
    "fs-extra": "^8.1.0",
    "html-webpack-plugin": "4.0.0-beta.5",
    "http-proxy-middleware": "^0.20.0",
    "identity-obj-proxy": "3.0.0",
    "jest": "24.9.0",
    "jest-environment-jsdom-fourteen": "0.1.0",
    "jest-resolve": "24.9.0",
    "jest-watch-typeahead": "0.4.2",
    "mini-css-extract-plugin": "0.8.0",
    "optimize-css-assets-webpack-plugin": "5.0.3",
    "pnp-webpack-plugin": "1.5.0",
    "postcss-flexbugs-fixes": "4.1.0",
    "postcss-loader": "3.0.0",
    "postcss-normalize": "8.0.1",
    "postcss-preset-env": "6.7.0",
    "postcss-safe-parser": "4.0.1",
    "prop-types": "^15.7.2",
    "react": "^16.12.0",
    "react-app-polyfill": "^1.0.5",
    "react-dev-utils": "^10.0.0",
    "react-dom": "^16.12.0",
    "react-redux": "^7.1.0",
    "react-router": "^5.1.2",
    "react-router-dom": "^5.1.2",
    "redux": "^4.0.4",
    "redux-devtools-extension": "^2.13.8",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.3.0",
    "resolve": "1.12.2",
    "resolve-url-loader": "3.1.1",
    "sass-loader": "8.0.0",
    "semver": "6.3.0",
    "style-loader": "1.0.0",
    "terser-webpack-plugin": "2.2.1",
    "ts-pnp": "1.1.5",
    "url-loader": "2.3.0",
    "webpack": "4.41.2",
    "webpack-dev-server": "3.9.0",
    "webpack-manifest-plugin": "2.2.0",
    "workbox-webpack-plugin": "4.3.1"
  },
  "scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js"
  },
  "eslintConfig": {
    "extends": ["react-app", "airbnb"],
    "rules": {
      "no-underscore-dangle": 0,
      "react/jsx-indent-props": [
        "error",
        4
      ],
      "react/jsx-indent": [
        "error",
        4
      ],
      "indent": [
        "error",
        4
      ],
      "semi": [
        "error",
        "always"
      ],
      "comma-dangle": [
        "error",
        {
          "arrays": "always-multiline",
          "objects": "always-multiline"
        }
      ]
    }
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "jest": {
    "roots": [
      "<rootDir>/src"
    ],
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!src/**/*.d.ts"
    ],
    "setupFiles": [
      "react-app-polyfill/jsdom"
    ],
    "setupFilesAfterEnv": [
      "<rootDir>/src/setupTests.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
      "<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
    ],
    "testEnvironment": "jest-environment-jsdom-fourteen",
    "transform": {
      "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
      "^.+\\.module\\.(css|sass|scss)$"
    ],
    "modulePaths": [],
    "moduleNameMapper": {
      "^react-native$": "react-native-web",
      "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "web.ts",
      "ts",
      "web.tsx",
      "tsx",
      "json",
      "web.jsx",
      "jsx",
      "node"
    ],
    "watchPlugins": [
      "jest-watch-typeahead/filename",
      "jest-watch-typeahead/testname"
    ]
  },
  "babel": {
    "presets": [
      "react-app"
    ]
  }
}

But when I run npm start I am getting the following error in the console:但是当我运行npm start时,我在控制台中收到以下错误:

Failed to compile.

./src/store/dashboards/reducers.js
  Line 29:43:  Unexpected dangling '_' in '_id'  no-underscore-dangle
  Line 36:36:  Unexpected dangling '_' in '_id'  no-underscore-dangle

Search for the keywords to learn more about each error.

I am using VS Code, and changing the eslintConfig property in package.json seems to work for the IDE, but not for running the app.我正在使用 VS Code,并且更改 package.json 中的 eslintConfig 属性似乎适用于 IDE,但不适用于运行应用程序。 I also tried adding: /* eslint-disable no-underscore-dangle */ to the top of ./src/store/dashboards/reducers.js .我还尝试添加: /* eslint-disable no-underscore-dangle */ ./src/store/dashboards/reducers.js /* eslint-disable no-underscore-dangle */./src/store/dashboards/reducers.js的顶部。 Again, it seems to register with the editor, but not the compiler.同样,它似乎注册到编辑器,而不是编译器。 I also tried extracting the eslint config to its own file .eslintrc .我还尝试将 eslint 配置提取到它自己的文件.eslintrc Still not compiling.还是不编译。

You can view the full repo here: https://github.com/bensoutendijk/mouseflow-react您可以在此处查看完整的存储库: https : //github.com/bensoutendijk/mouseflow-react

@devaent 's solution (re-installing node_modules and yarn.lock ) helped a lot but I had to re-do it every time I add another rule to .eslintrc.json . @devaent的解决方案(重新安装node_modulesyarn.lock )有很大帮助,但每次我向.eslintrc.json添加另一条规则时,我都必须重新做。

I had been suffering from it for last 3 days and thankfully found another solution today.过去 3 天我一直在受苦,幸好今天找到了另一个解决方案。

On line 340 of config/webpack.config.js add useEslintrc: true as below and it worked for me.config/webpack.config.js第 340 行添加useEslintrc: true如下,它对我useEslintrc: true

options: {
                cache: true,
                formatter: require.resolve('react-dev-utils/eslintFormatter'),
                eslintPath: require.resolve('eslint'),
                resolvePluginsRelativeTo: __dirname,
                useEslintrc: true,                   // <= added line
              },

I got this idea from this article [ link ].我从这篇文章 [ 链接] 中得到了这个想法。

FYI, I share my .eslintrc.json below.仅供参考,我在下面分享我的.eslintrc.json

{
  "env": {
    "browser": true,
    "commonjs": true,
    "es2020": true
  },
  "extends": ["react-app", "plugin:react/recommended", "airbnb"],
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 11
  },
  "plugins": ["react"],
  "rules": {
    "no-unused-vars": 0,
    "react/jsx-filename-extension": 0,
    "linebreak-style": 0,
    "jsx-quotes": 0,
    "object-curly-newline": 0,
    "react/prop-types": 0,
    "react/destructuring-assignment": 0,
    "comma-dangle": 0,
    "arrow-body-style": 0,
    "implicit-arrow-linebreak": 0,
    "no-else-return": 0,
    "array-bracket-spacing": 0,
    "react/button-has-type": 0,
    "import/prefer-default-export": 0,
    "func-names": 0,
    "no-underscore-dangle": 0,
    "react/jsx-one-expression-per-line": 0,
    "operator-linebreak": 0,
    "jsx-a11y/media-has-caption": 0
  }
}

I was able to solve the issue by deleting /node_modules/ and package-lock.json我能够通过删除 /node_modules/ 和 package-lock.json 来解决这个问题

However, the issue keeps popping up unfortunately.然而,不幸的是,这个问题不断出现。 If anyone has an explanation, I will accept that as the answer.如果有人有解释,我会接受它作为答案。

Edit: Final Verdict, npm uninstall -g eslint编辑:最终判决, npm uninstall -g eslint

It seems that the eslint errors are breaking the build.似乎 eslint 错误正在破坏构建。 You can disable it by adding "emitWarning: true" in the eslint-loader configuration at webpack.config.js您可以通过在 webpack.config.js 的 eslint-loader 配置中添加“emitWarning: true”来禁用它

 {
      test: /\.(js|mjs|jsx|ts|tsx)$/,
      enforce: 'pre',
      use: [
        {
          options: {
            cache: true,
            formatter: require.resolve('react-dev-utils/eslintFormatter'),
            eslintPath: require.resolve('eslint'),
            resolvePluginsRelativeTo: __dirname,
            emitWarning: true,
          },
          loader: require.resolve('eslint-loader'),
        },
      ],
      include: paths.appSrc,
    },

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

相关问题 有没有办法在不弹出的情况下为 create-react-app 更改 webpack 的 eslint - Is there a way to change the eslint of the webpack for the create-react-app without ejecting 添加react-hot-loader以弹出create-react-app - Adding react-hot-loader to ejected create-react-app 使用弹出的create-react-app创建SVG Sprite组件 - Creating an SVG Sprite component with ejected create-react-app 未弹出时,create-react-app应用程序会将babel配置存储在哪里? - Where does create-react-app application store it's babel configuration when not ejected? 安装 npm create-react-app 后 ESLint 出现问题 - Problem with ESLint after installing npm create-react-app ESLint 仅针对特定目录(eslintrc、create-react-app) - ESLint only target a specific directory (eslintrc, create-react-app) 如何扩展 Eslint 以使用 create-react-app - How to Extend Eslint to work with create-react-app ESLint 错误/警告应阻止在 React (create-react-app) 中编译 - ESLint errors/warnings should prevent compiling in React (create-react-app) 有没有办法在没有 create-react-app 的情况下在 React 中导入模块? - Is there a way to import modules in React without create-react-app? Eslint precommit husky hook在create-react-app项目中未按预期工作 - Eslint precommit husky hook is not working as expected in create-react-app project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM