简体   繁体   English

如何扩展 Eslint 以使用 create-react-app

[英]How to Extend Eslint to work with create-react-app

I'm working on a React application and I would like to have a linter set up so that I can see all the warning/errors in the console.我正在开发一个 React 应用程序,我想设置一个 linter,以便我可以在控制台中看到所有警告/错误。

The docs doesn't say much: https://create-react-app.dev/docs/setting-up-your-editor/文档没有说太多: https : //create-react-app.dev/docs/setting-up-your-editor/

I have added EXTEND_ESLINT=true in my .env.dev file and I have created a .eslintrc.json file as well, with the following content (taken from the docs):我在我的.env.dev文件中添加了EXTEND_ESLINT=true并且我还创建了一个.eslintrc.json文件,其中包含以下内容(取自文档):

{
  "eslintConfig": {
    "extends": ["react-app", "shared-config"],
    "rules": {
      "additional-rule": "warn"
    },
    "overrides": [
      {
        "files": ["**/*.ts?(x)"],
        "rules": {
          "additional-typescript-only-rule": "warn"
        }
      }
    ]
  }
}

Every rule I try to add won't do anything, I still see no warnings in the console and on top of that if I try to run the linter from the command line:我尝试添加的每条规则都不会做任何事情,我仍然在控制台中看不到警告,最重要的是,如果我尝试从命令行运行 linter:

npx eslint ./src

I get the following error:我收到以下错误:

ESLint configuration in .eslintrc.json is invalid:
    - Unexpected top-level property "eslintConfig".

What am I missing?我错过了什么?

You can either create a .eslintrc.js file inside your src folder, with this syntax:您可以使用以下语法在src文件夹中创建一个.eslintrc.js文件:

module.exports = {
    extends: ["react-app", "shared-config"],
    rules: {
      "additional-rule": "warn"
    },
    overrides: [
      {
        "files": ["**/*.ts?(x)"],
        "rules": {
          "additional-typescript-only-rule": "warn"
        }
      }
    ]
  }

Or add this to your package.json (not a .eslintrc.json file):或者将其添加到您的package.json (不是 .eslintrc.json 文件):

"eslintConfig": {
    "extends": ["react-app", "shared-config"],
    "rules": {
      "additional-rule": "warn"
    },
    "overrides": [
      {
        "files": ["**/*.ts?(x)"],
        "rules": {
          "additional-typescript-only-rule": "warn"
        }
      }
    ]
  }

您可以简单地在 package.json 中添加插件规则和配置,而无需创建 .eslintrc.json 和 env 变量

暂无
暂无

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

相关问题 ESLint 仅针对特定目录(eslintrc、create-react-app) - ESLint only target a specific directory (eslintrc, create-react-app) 有没有办法在不弹出的情况下为 create-react-app 更改 webpack 的 eslint - Is there a way to change the eslint of the webpack for the create-react-app without ejecting 安装 npm create-react-app 后 ESLint 出现问题 - Problem with ESLint after installing npm create-react-app 有没有办法更改弹出的 create-react-app 的 eslint 规则? - Is there a way to change eslint rules for ejected create-react-app? ESLint 错误/警告应阻止在 React (create-react-app) 中编译 - ESLint errors/warnings should prevent compiling in React (create-react-app) 如何在create-react-app中使用引导程序? - How to use bootstrap in create-react-app? create-react-app eslint 错误“解析错误:‘import’和‘export’可能只出现在‘sourceType:module’中” - create-react-app eslint error "Parsing error: 'import' and 'export' may appear only with 'sourceType: module'" Eslint precommit husky hook在create-react-app项目中未按预期工作 - Eslint precommit husky hook is not working as expected in create-react-app project ESLint 是如何集成到 Create React App 中的? - How is ESLint integrated into Create React App? 使用 Create-React-App 创建 React 应用程序失败并出现 ERR:在“…er”附近解析时,JSON 输入意外结束。”^2.0,0“,”eslint”' - Create React app with Create-React-App fails with ERR! Unexpected end of JSON input while parsing near '…er“:”^2.0.0“,”eslint"'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM