首次创建React Native项目遇npm安装错误,求终端解决命令
React Native 创建项目报错解决方案
1. 清理损坏的npm缓存
npm cache clean --force npm cache verify
作用:解决invalid json response body报错,清除npm缓存中损坏的包文件
2. 卸载废弃的旧版CLI
npm uninstall -g react-native-cli
作用:旧版react-native-cli已被官方废弃,会导致依赖解析异常
3. 使用官方推荐方式创建项目
推荐:用npx指定版本创建
npx react-native@0.70.6 init TestApp --version 0.70.6
作用:直接使用官方维护的CLI,指定报错中对应的react-native@0.70.6版本,避免版本不兼容
备选:用yarn规避npm网络问题
先安装yarn:
npm install -g yarn
再创建项目:
yarn create react-native-app TestApp --template react-native-template-typescript@0.70.6
4. 手动修复缺失的依赖(若仍有警告)
如果创建后仍出现react@undefined警告,手动安装指定版本的React:
npm install react@18.1.0 --save-exact
内容的提问来源于stack exchange,提问作者Saeb Khanzadeh
相关产品推荐
相关产品推荐

