[英]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.