使用npx react-native init时遇npm ERR! could not resolve错误求解决
React Native CLI 创建项目时的npm依赖冲突解决方案
按照React Native官方文档指引,使用npx react-native init newApp创建新项目时出现npm依赖解析错误,错误日志如下:
PS D:\Android development> npx react-native init newApp npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead. Need to install the following packages: react-native@0.70.3 Ok to proceed? (y) y npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: undefined@undefined npm ERR! Found: react-native@0.68.1 npm ERR! node_modules/react-native npm ERR! peer react-native@"*" from @react-native-community/cli@7.0.3 npm ERR! node_modules/@react-native-community/cli npm ERR! @react-native-community/cli@"^7.0.3" from react-native@0.68.1 npm ERR! react-native@"0.70.3" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! react-native@"0.70.3" from the root project npm ERR! npm ERR! Conflicting peer dependency: react@18.1.0 npm ERR! node_modules/react npm ERR! peer react@"18.1.0" from react-native@0.70.3 npm ERR! node_modules/react-native npm ERR! react-native@"0.70.3" 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\abhad\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\abhad\AppData\Local\npm-cache\_logs\2022-10-13T07_58_54_900Z-debug-0.log
可行解决方案:
使用
--legacy-peer-deps参数绕过peer依赖检查,执行命令:npx react-native init newApp --legacy-peer-deps该参数让npm采用旧版peer依赖解析逻辑,避免版本冲突报错。
清理本地npm缓存后重新执行:
- 清理缓存:
npm cache clean --force - 重新初始化项目:
npx react-native init newApp
缓存中的旧依赖版本可能引发冲突,清理后重新拉取依赖可解决问题。
- 清理缓存:
指定兼容的React Native版本并配合参数:
选择日志中已存在的稳定版本(如0.68.1)执行:npx react-native init newApp --version 0.68.1 --legacy-peer-deps更新npm到最新版本:
npm install -g npm@latest新版npm的依赖解析逻辑可能修复此类冲突问题。
内容的提问来源于stack exchange,提问作者Aritro
相关产品推荐
相关产品推荐

