与我的团队一起,使用create-react-app bootstrap命令启动一个新的React Project。 我们在项目上添加了eslint部分,但由于棘手的错误而陷入困境,而之前从未发现过。 当我们启动命令
yarn run lint
在这里package.json:
{
"name": "name of the app",
"version": "0.1.0",
"private": true,
"dependencies": {
"jwt-decode": "^2.2.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-intl": "^3.12.0",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.0",
"redux": "^4.0.5",
"redux-saga": "^1.1.3",
"reselect": "^4.0.0",
"styled-components": "^5.0.1"
},
"scripts": {
"analyze": "react-app-rewired build",
"build": "react-scripts build",
"cypress": "./node_modules/.bin/cypress open",
"eject": "react-scripts eject",
"lint:write": "eslint --debug src/ --fix",
"lint": "eslint --debug src/",
"prettier": "prettier --write src/*.tsx src/*.ts src/**/*.tsx src/**/*.ts src/**/**/*.tsx src/**/**/*.ts src/**/**/**/*.tsx src/**/**/**/*.ts src/**/**/**/**/*.tsx src/**/**/**/**/*.ts",
"start": "react-scripts start",
"storybook": "start-storybook -p 6006 -c .storybook",
"test": "react-scripts test",
"upgrade:check": "npm-check",
"upgrade:interactive": "npm-check --update"
},
"eslintConfig": {
"extends": "react-app"
},
"lint-staged": {
"*.(ts|tsx)": [
"yarn run prettier"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@cypress/webpack-preprocessor": "^4.1.2",
"@storybook/addon-actions": "^5.3.12",
"@storybook/addon-info": "^5.3.12",
"@storybook/addon-knobs": "^5.3.12",
"@storybook/addon-links": "^5.3.12",
"@storybook/addons": "^5.3.12",
"@storybook/preset-create-react-app": "^1.5.2",
"@storybook/react": "^5.3.12",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^24.0.0",
"@types/jwt-decode": "^2.2.1",
"@types/node": "^12.0.0",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/react-redux": "^7.1.7",
"@types/react-router": "^5.1.4",
"@types/react-router-dom": "^5.1.3",
"@types/storybook__addon-knobs": "^5.2.1",
"@types/styled-components": "^4.4.2",
"cypress": "^4.0.1",
"eslint": "^6.8.0",
"husky": "^4.2.1",
"lint-staged": "^10.0.7",
"npm-check": "^5.9.0",
"prettier": "^1.19.1",
"react-app-rewire-webpack-bundle-analyzer": "^1.1.0",
"react-app-rewired": "^2.1.5",
"react-docgen-typescript-webpack-plugin": "^1.1.0",
"redux-devtools-extension": "^2.13.8",
"storybook-addon-jsx": "^7.1.14",
"ts-loader": "^6.2.1",
"typescript": "~3.7.2",
"webpack": "^4.41.6",
"webpack-bundle-analyzer": "^3.6.0"
}
}
我们还尝试添加创建.eslintrc文件并添加
{
"extends": "react-app"
}
但是什么都没有改变。
答案 0 :(得分:9)
react-scripts
应该已经在节点依赖项中包含ESLint和eslint-config-react-app
ESLint配置(请参见docs)。
您不需要在Create React App项目中单独安装它。
如果ESLint无法找到react-app
配置,则可能是在react-scripts
之外(即另一个eslint-config-react-app
安装之外)安装的冲突或重复的ESLint配置依赖项。您可以使用npm ls [package]
命令(即npm ls eslint-config-react-app
)进行检查。
解决此问题的最佳方法可能是从package.json
中删除那些软件包,以允许按预期使用eslint-config-react-app
中的react-scripts
之类的配置(基于comment )。如果之后问题仍然存在,请尝试删除node_modules
文件夹和package-lock.json
,然后重新安装依赖项可能会起作用。否则,升级到新的npm
版本可能会有所不同。
您的package.json
可能缺少对等方依存关系eslint-config-react-app
,其中包括随行预设react-app
。您需要按照说明在此处https://www.npmjs.com/package/eslint-config-react-app进行安装。
注意:即使这是create-react-app
并将其捆绑到react-scripts
上,您的lint
脚本仍无法访问react-app
。
"eslintConfig": {
"extends": "react-app"
}
关于.eslintrc
,您已经在package.json
中拥有ESLint的配置,因此应该安全地删除.eslintrc
。
答案 1 :(得分:6)
这里的问题在于 yarn
包管理器。因为eslint-config-react-app
不是直接在 node_modules
下,而是在 react-scripts 中:node_modules\react-scripts\node_modules
纱线没有正确加载(更多信息 here)
因此,目前有两种方法可以解决该问题:
npm
而不是 yarn
。eslint-config-react-app
中的软件包外,还安装 react-scripts
软件包。答案 2 :(得分:1)
我通过使用 npm install
而不是 yarn
解决了这个问题。
不知道为什么,但我猜是因为 yarn 一次安装多个包,而 npm 一个接一个安装它们。所以也许其中一些被跳过或没有正确安装。
答案 3 :(得分:0)
已解决!
我解决了更改lint脚本的问题:
旧的:
"lint": "eslint --debug src/"
对此是新手:
"lint": "eslint --debug 'src/**/*.{js,jsx,ts,tsx}'",