You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

react-native init生成的项目中ESLint无法正常运行问题求助

React Native项目ESLint加载prettier插件失败解决步骤

报错根因

错误核心为两个问题:

  1. React Native初始化项目未自动安装eslint-plugin-prettier依赖
  2. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 22:15:09