react-native init生成的项目中ESLint无法正常运行问题求助
React Native项目ESLint加载prettier插件失败解决步骤
报错根因
错误核心为两个问题:
- React Native初始化项目未自动安装
eslint-plugin-prettier依赖 - ESLint优先加载了用户根目录下全局安装的
@react-native-community/eslint-config,和项目依赖版本不匹配,导致依赖查找失败
解决操作
1. 安装项目缺失依赖
在项目根目录(你初始化的Bar文件夹)下执行对应命令:
- 使用npm安装:
npm install eslint-plugin-prettier @react-native-community/eslint-config --save-dev
如果M1芯片环境下出现架构兼容或权限报错,添加--force参数执行:
npm install eslint-plugin-prettier @react-native-community/eslint-config --save-dev --force
- 使用yarn安装:
yarn add eslint-plugin-prettier @react-native-community/eslint-config --dev
2. 删除全局冗余ESLint相关依赖
报错日志显示ESLint引用了用户根目录下的/Users/myuser/node_modules/@react-native-community/eslint-config,全局安装的包会优先被加载,和项目依赖不匹配,需要删除:
- 执行命令删除用户根目录下的冗余node_modules文件夹:
rm -rf ~/node_modules
- 检查全局npm安装的ESLint相关包,存在就卸载:
# 查看全局安装的相关包 npm ls -g eslint @react-native-community/eslint-config # 如果有输出对应包,执行卸载 npm uninstall -g eslint @react-native-community/eslint-config
3. 重启ESLint服务
- 按
Cmd+Shift+P调出VSCode命令面板,输入ESLint: Restart ESLint Server执行,重启ESLint服务即可生效 - 如果仍未生效,直接关闭VSCode重新打开项目即可
4. M1芯片环境适配(可选)
如果操作完仍报错,检查当前node版本的架构是否适配:
- 执行
node -p "process.arch",确认输出为arm64,如果输出为x64说明当前node是通过Rosetta转译运行的,重新安装arm64适配的LTS版本node即可。
内容的提问来源于stack exchange,提问作者lotaquestions
相关产品推荐
相关产品推荐

