无法安装Native Base:依赖树冲突问题求助
解决Native Base安装时的ERESOLVE依赖冲突问题
使用Node.js v18.9.1和React Native Expo 6.0.8环境,执行npm install native-base命令安装Native Base时,触发如下依赖树解析错误:
D:\Ui-Project\UI-Project>npm install native-base npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: ui-project@1.0.0 npm ERR! Found: react@"18.1.0" npm ERR! node_modules/react npm ERR! react@"18.1.0" from the root project npm ERR! peer react@"*" from native-base@3.4.21 npm ERR! node_modules/native-base npm ERR! native-base@"*" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer react@"^18.2.0" from react-dom@18.2.0 npm ERR! node_modules/react-dom npm ERR! peer react-dom@"*" from native-base@3.4.21 npm ERR! node_modules/native-base npm ERR! native-base@"*" 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! See C:\Users\PC\AppData\Local\npm-cache\eresolve-report.txt for a full report. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\PC\AppData\Local\npm-cache\_logs\2022-11-15T14_51_31_860Z-debug-0.log
核心原因
项目当前使用的react@18.1.0版本,与react-dom@18.2.0要求的peer依赖react@^18.2.0不匹配,而Native Base同时依赖这两个包,导致npm无法解析出合法的依赖树。
解决方案
1. 升级React到兼容版本
直接将React升级到react-dom@18.2.0要求的版本范围:
npm install react@^18.2.0
升级完成后,重新执行npm install native-base即可正常安装。
2. 使用npm兼容参数跳过peer依赖检查
如果暂时不想升级React,可使用--legacy-peer-deps参数忽略peer依赖冲突(该方式在React Native项目中较为常用,风险较低):
npm install native-base --legacy-peer-deps
也可以用--force强制安装,但可能引入潜在的兼容性问题,不推荐优先使用:
npm install native-base --force
3. 锁定依赖版本(可选)
为避免后续依赖自动升级引发同类问题,可在package.json中明确指定React和React DOM的版本保持一致:
{ "dependencies": { "react": "^18.2.0", "react-dom": "^18.2.0" } }
修改后执行npm install同步依赖即可。
内容的提问来源于stack exchange,提问作者Tuấn Trương
相关产品推荐
相关产品推荐

