简体   繁体   English

为什么默认的 react app 文件在启用 eslint 的情况下无法运行?

[英]Why does the default react app file not run with eslint enabled?

I have globally installed eslint via cmd and its also installed as an extension in vs code.我已经通过 cmd 全局安装了 eslint,它也作为 vs 代码的扩展安装。 After that I used the following command in the vs code terminal to initialize it:之后,我在 vs code 终端中使用以下命令对其进行初始化:

npx eslint --init 

I used the Airbnb style guide option while setting it up.我在设置时使用了 Airbnb 风格指南选项。 But even running the default file that comes when you create a react app multiple errors are shown like this screenshot of errors .但是,即使运行创建 React 应用程序时附带的默认文件,也会显示多个错误,如错误截图所示。
I have just started learning React, and don't know what other details might be needed to solve this issue.我刚开始学习 React,不知道解决这个问题可能需要哪些其他细节。 Below is the App.js file code.下面是 App.js 文件代码。

 import React from 'react'; import logo from './logo.svg'; import './App.css'; function App() { return ( < div className = "App" > < header className = "App-header" > < img src = { logo } className = "App-logo" alt = "logo" / > < p > Edit < code > src / App.js < /code> and save to reload. < /p> < a className = "App-link" href = "https://reactjs.org" target = "_blank" rel = "noopener noreferrer" > Learn React < /a> < /header> < /div> ); } export default App;

Edit: Imported React module and added编辑:导入 React 模块并添加

 "react/jsx-filename-extension": [1, { "extensions": [".js", ".JSX"] }]

in rules of.eslinrc.json but在.eslinrc.json 的规则中,但是

Line 7:5:  JSX not allowed in files with extension '.js'  react/jsx-filename-extension 

this error is still there, despite deleting and reinitializing eslint for my app.尽管为我的应用程序删除并重新初始化了 eslint,但此错误仍然存在。

This issue has been fixed in the react-scipts:"4.0.3" but, the eslint errors present in the project are not converted to warnings by default.此问题已在 react-scipts:"4.0.3" 中修复,但默认情况下项目中存在的 eslint 错误不会转换为警告。 You have to create an.env file which should contain a ESLINT_NO_DEV_ERRORS=true flag.您必须创建一个应包含ESLINT_NO_DEV_ERRORS=true标志的 .env 文件。 Due to this flag, you will receive the eslint errors as warnings and not as errors in the development.由于这个标志,您将收到 eslint 错误作为警告,而不是开发中的错误。

This flag is ignored during production and when they are any git hooks running, which will in turn cause an error when you are trying to commit the code with an eslint error in it.该标志在生产过程中被忽略,并且当它们运行任何 git 挂钩时,当您尝试提交带有 eslint 错误的代码时,这反过来会导致错误。

Add extends: ["eslint:recommended", "plugin:react/recommended"] to the eslint config, and install the plugin using npm install eslint-plugin-react .extends: ["eslint:recommended", "plugin:react/recommended"]添加到 eslint 配置中,并使用npm install eslint-plugin-react

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

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