React Native安装依赖包时出现无法解析依赖错误求助
解决React Native项目中的ERESOLVE依赖冲突问题
问题背景
在React Native项目中执行以下命令安装导航相关依赖时,触发了ERESOLVE依赖解析错误:
npx expo install @react-navigation/native@5.9.8 @react-navigation/stack@5.14.9 react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view expo-constants
核心冲突为:react-native-elements@3.4.2要求对等依赖react-native-safe-area-context@^3.1.9,但当前项目已安装的是4.3.1。尝试执行npm install react@latest更新React后,仍出现相同的依赖冲突错误。
安装依赖时的错误日志
npm WARN ERESOLVE overriding peer dependency npm WARN While resolving: nucampsite@1.0.0 npm WARN Found: react-native-safe-area-context@3.4.1 npm WARN node_modules/react-native-safe-area-context npm WARN react-native-safe-area-context@"4.3.1" from the root project npm WARN 2 more (react-native-elements, @react-navigation/stack) npm WARN npm WARN Could not resolve dependency: npm WARN peer react-native-safe-area-context@"^3.1.9" from react-native-elements@3.4.2 npm WARN node_modules/react-native-elements npm WARN react-native-elements@"3.4.2" from the root project npm WARN deprecated @react-native-community/masked-view@0.1.11: Repository was moved to @react-native-masked-view/masked-view added 25 packages, removed 1 package, changed 2 packages, and audited 1185 packages in 12s 54 packages are looking for funding run `npm fund` for details 6 high severity vulnerabilities To address all issues (including breaking changes), run: npm audit fix --force Run `npm audit` for details. > npm install --save @react-native-community/masked-view npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: react-native-elements@3.4.2 npm ERR! Found: react-native-safe-area-context@4.3.1 npm ERR! node_modules/react-native-safe-area-context npm ERR! peer react-native-safe-area-context@">= 0.6.0" from @react-navigation/stack@5.14.9 npm ERR! node_modules/@react-navigation/stack npm ERR! @react-navigation/stack@"5.14.9" from the root project npm ERR! react-native-safe-area-context@"4.3.1" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer react-native-safe-area-context@"^3.1.9" from react-native-elements@3.4.2 npm ERR! node_modules/react-native-elements npm ERR! react-native-elements@"3.4.2" from the root project npm ERR! npm ERR! Conflicting peer dependency: react-native-safe-area-context@3.4.1 npm ERR! node_modules/react-native-safe-area-context npm ERR! peer react-native-safe-area-context@"^3.1.9" from react-native-elements@3.4.2 npm ERR! node_modules/react-native-elements npm ERR! react-native-elements@"3.4.2" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! C:\Users\isena\AppData\Local\npm-cache\_logs\2022-12-09T22_33_54_680Z-eresolve-report.txt npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\isena\AppData\Local\npm-cache\_logs\2022-12-09T22_33_54_680Z-debug-0.log
项目package.json内容
{ "name": "nucampsite", "version": "1.0.0", "main": "node_modules/expo/AppEntry.js", "scripts": { "start": "expo start", "android": "expo start --android", "ios": "expo start --ios", "web": "expo start --web" }, "dependencies": { "expo": "^46.0.13", "expo-status-bar": "^1.4.0", "react": "^18.0.0", "react-native": "^0.69.6", "react-native-elements": "3.4.2", "@react-navigation/native": "5.9.8", "@react-navigation/stack": "5.14.9", "react-native-reanimated": "~2.9.1", "react-native-gesture-handler": "~2.5.0", "react-native-screens": "~3.15.0", "react-native-safe-area-context": "4.3.1", "expo-constants": "~13.2.4" }, "devDependencies": { "@babel/core": "^7.12.9" }, "private": true }
更新React时的错误日志
npm install react@latest npm WARN ERESOLVE overriding peer dependency npm WARN While resolving: nucampsite@1.0.0 npm WARN Found: react@18.0.0 npm WARN node_modules/react npm WARN peer react@">=16.0" from @react-native-community/masked-view@0.1.11 npm WARN node_modules/@react-native-community/masked-view npm WARN peer @react-native-community/masked-view@">= 0.1.0" from @react-navigation/stack@5.14.9 npm WARN node_modules/@react-navigation/stack npm WARN 13 more (@react-navigation/core, @react-navigation/native, ...) npm WARN npm WARN Could not resolve dependency: npm WARN peer react@"18.0.0" from react-native@0.69.6 npm WARN node_modules/react-native npm WARN peer react-native@"*" from @react-native-community/cli@8.0.6 npm WARN node_modules/@react-native-community/cli npm WARN 11 more (@react-native-community/masked-view, ...) npm WARN ERESOLVE overriding peer dependency npm WARN While resolving: nucampsite@1.0.0 npm WARN Found: react@18.0.0 npm WARN node_modules/react npm WARN peer react@">=16.0" from @react-native-community/masked-view@0.1.11 npm WARN node_modules/@react-native-community/masked-view npm WARN peer @react-native-community/masked-view@">= 0.1.0" from @react-navigation/stack@5.14.9 npm WARN node_modules/@react-navigation/stack npm WARN 13 more (@react-navigation/core, @react-navigation/native, ...) npm WARN npm WARN Could not resolve dependency: npm WARN peer react@"18.0.0" from react-native@0.69.6 npm WARN node_modules/react-native npm WARN peer react-native@"*" from @react-native-community/cli@8.0.6 npm WARN node_modules/@react-native-community/cli npm WARN 11 more (@react-native-community/masked-view, ...) npm WARN ERESOLVE overriding peer dependency npm WARN While resolving: nucampsite@1.0.0 npm WARN Found: react@18.0.0 npm WARN node_modules/react npm WARN peer react@">=16.0" from @react-native-community/masked-view@0.1.11 npm WARN node_modules/@react-native-community/masked-view npm WARN peer @react-native-community/masked-view@">= 0.1.0" from @react-navigation/stack@5.14.9 npm WARN node_modules/@react-navigation/stack npm WARN 13 more (@react-navigation/core, @react-navigation/native, ...) npm WARN npm WARN Could not resolve dependency: npm WARN peer react@"18.0.0" from react-native@0.69.6 npm WARN node_modules/react-native npm WARN peer react-native@"*" from @react-native-community/cli@8.0.6 npm WARN node_modules/@react-native-community/cli npm WARN 11 more (@react-native-community/masked-view, ...) npm WARN ERESOLVE overriding peer dependency npm WARN While resolving: nucampsite@1.0.0 npm WARN Found: react@18.0.0 npm WARN node_modules/react npm WARN peer react@">=16.0" from @react-native-community/masked-view@0.1.11 npm WARN node_modules/@react-native-community/masked-view npm WARN peer @react-native-community/masked-view@">= 0.1.0" from @react-navigation/stack@5.14.9 npm WARN node_modules/@react-navigation/stack npm WARN 13 more (@react-navigation/core, @react-navigation/native, ...) npm WARN npm WARN Could not resolve dependency: npm WARN peer react@"18.0.0" from react-native@0.69.6 npm WARN node_modules/react-native npm WARN peer react-native@"*" from @react-native-community/cli@8.0.6 npm WARN node_modules/@react-native-community/cli npm WARN 11 more (@react-native-community/masked-view, ...) npm WARN ERESOLVE overriding peer dependency npm WARN While resolving: nucampsite@1.0.0 npm WARN Found: react@18.0.0 npm WARN node_modules/react npm WARN peer react@">=16.0" from @react-native-community/masked-view@0.1.11 npm WARN node_modules/@react-native-community/masked-view npm WARN peer @react-native-community/masked-view@">= 0.1.0" from @react-navigation/stack@5.14.9 npm WARN node_modules/@react-navigation/stack npm WARN 13 more (@react-navigation/core, @react-navigation/native, ...) npm WARN npm WARN Could not resolve dependency: npm WARN peer react@"18.0.0" from react-native@0.69.6 npm WARN node_modules/react-native npm WARN peer react-native@"*" from @react-native-community/cli@8.0.6 npm WARN node_modules/@react-native-community/cli npm WARN 11 more (@react-native-community/masked-view, ...) npm WARN ERESOLVE overriding peer dependency npm WARN While resolving: nucampsite@1.0.0 npm WARN Found: react@18.0.0 npm WARN node_modules/react npm WARN peer react@">=16.0" from @react-native-community/masked-view@0.1.11 npm WARN node_modules/@react-native-community/masked-view npm WARN peer @react-native-community/masked-view@">= 0.1.0" from @react-navigation/stack@5.14.9 npm WARN node_modules/@react-navigation/stack npm WARN 13 more (@react-navigation/core, @react-navigation/native, ...) npm WARN npm WARN Could not resolve dependency: npm WARN peer react@"18.0.0" from react-native@0.69.6 npm WARN node_modules/react-native npm WARN peer react-native@"*" from @react-native-community/cli@8.0.6 npm WARN node_modules/@react-native-community/cli npm WARN 11 more (@react-native-community/masked-view, ...) npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: react-native-elements@3.4.2 npm ERR! Found: react-native-safe-area-context@4.3.1 npm ERR! node_modules/react-native-safe-area-context npm ERR! peer react-native-safe-area-context@">= 0.6.0" from @react-navigation/stack@5.14.9 npm ERR! node_modules/@react-navigation/stack npm ERR! @react-navigation/stack@"5.14.9" from the root project npm ERR! react-native-safe-area-context@"4.3.1" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer react-native-safe-area-context@"^3.1.9" from react-native-elements@3.4.2 npm ERR! node_modules/react-native-elements npm ERR! react-native-elements@"3.4.2" from the root project npm ERR! npm ERR! Conflicting peer dependency: react-native-safe-area-context@3.4.1 npm ERR! node_modules/react-native-safe-area-context npm ERR! peer react-native-safe-area-context@"^3.1.9" from react-native-elements@3.4.2 npm ERR! node_modules/react-native-elements npm ERR! react-native-elements@"3.4.2" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
解决方案
方案1:降级react-native-safe-area-context到兼容版本
由于react-native-elements@3.4.2仅支持react-native-safe-area-context@^3.1.9,将其降级到3.x系列的最新兼容版本:
npm install react-native-safe-area-context@3.4.1 --save
执行完成后,重新运行最初的依赖安装命令即可。
方案2:升级react-native-elements到支持更高版本依赖的版本
react-native-elements的4.x及以上版本已支持更高版本的react-native-safe-area-context,直接升级:
npm install react-native-elements@latest --save
升级完成后,重新安装导航相关依赖。
方案3:使用--legacy-peer-deps参数绕过冲突
如果暂时不想修改依赖版本,可使用npm的--legacy-peer-deps参数强制跳过对等依赖检查:
npx expo install @react-navigation/native@5.9.8 @react-navigation/stack@5.14.9 react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view expo-constants --legacy-peer-deps
注意:此方法可能存在潜在兼容性问题,仅作为临时解决方案。
额外优化:替换废弃的masked-view包
错误日志提示@react-native-community/masked-view已废弃,迁移至@react-native-masked-view/masked-view,执行以下命令替换:
npm uninstall @react-native-community/masked-view && npm install @react-native-masked-view/masked-view --save
内容的提问来源于stack exchange,提问作者IsabelleNM
相关产品推荐
相关产品推荐

