如何在Expo Go项目中不使用--force或--legacy-peer-deps解决上游依赖冲突
问题背景
在Expo Go项目执行npm install时触发ERESOLVE依赖冲突错误,核心矛盾是:当前项目依赖react-native-reanimated@~2.14.4,但react-native-reanimated-carousel@3.5.1要求peer依赖react-native-reanimated >=3.0.0,版本不兼容导致依赖解析失败。同时执行eas build时,也因npm install exited with non-zero code: 1而失败。
使用--force或--legacy-peer-deps参数(包括全局设置npm config set legacy-peer-deps true)可暂时跳过冲突,但这类方法属于妥协方案,可能引发运行时未知bug,并非根本解决途径。
根本解决步骤
1. 定位完整依赖树
先执行命令查看所有依赖react-native-reanimated的包,理清冲突关联的模块:
npm ls react-native-reanimated
该命令会输出依赖树中所有直接/间接依赖该包的模块,帮你明确版本依赖关系。
2. 选择适配的解决方法
根据项目需求,可任选以下方案:
方案一:移除冲突包(已验证有效)
若react-native-reanimated-carousel并非项目必需模块,直接卸载即可解决冲突:
npm uninstall react-native-reanimated-carousel
卸载后重新执行npm install,依赖即可正常安装,eas build也能顺利执行。
方案二:升级核心依赖版本
若需要保留react-native-reanimated-carousel,可尝试升级react-native-reanimated到满足要求的版本(如3.5.4),同时验证其他依赖是否兼容新版本:
npm install react-native-reanimated@^3.5.4
升级后需测试项目功能,确保@react-navigation/drawer、react-native-circular-progress-indicator等其他依赖能正常工作。
方案三:降级冲突包版本
查找react-native-reanimated-carousel中支持react-native-reanimated@2.x的版本,安装对应版本适配现有依赖:
npm install react-native-reanimated-carousel@<兼容2.x的版本号>
可通过查看该包的版本历史或文档,确认兼容2.x版本的具体版本号。
内容的提问来源于stack exchange,提问作者Patrick Lay

