M1 Mac Ventura下React Native CLI初始化项目遇CocoaPods依赖错误
解决React Native init时CocoaPods依赖安装失败问题
问题背景
首次搭建React Native开发环境,已按官方指南完成CLI配置,执行npx react-native init AwesomeProject1时,在“Installing CocoaPods dependencies”步骤耗时极久并报错,提示存在多个Podfile且iOS环境未正确配置。系统配置:M1芯片MacOS Ventura 13.2,watchman 2023.02.06.00,Homebrew 3.6.21,Node v19.6.0,已安装最新Xcode并配置好命令行工具路径。
错误详情:
✔ Downloading template ✔ Copying template ✔ Processing template ✔ Installing Bundler ✖ Installing CocoaPods dependencies (this may take a few minutes) error warn Multiple Podfiles were found: ios/Podfile,vendor/bundle/ruby/2.7.0/gems/cocoapods-core-1.11.3/lib/cocoapods-core/Podfile. Choosing ios/Podfile automatically. If you would like to select a different one, you can configure it via "project.ios.sourceDir". You can learn more about it here: https://github.com/react-native-community/cli/blob/master/docs/configuration.md ✖ Installing CocoaPods dependencies (this may take a few minutes) error Error: Looks like your iOS environment is not properly set. Please go to https://reactnative.dev/docs/next/environment-setup and follow the React Native CLI QuickStart guide for macOS and iOS. Error: Error: Looks like your iOS environment is not properly set. Please go to https://reactnative.dev/docs/next/environment-setup and follow the React Native CLI QuickStart guide for macOS and iOS. at createFromTemplate (/Users/niteshrastogi/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/commands/init/init.js:129:11) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Object.initialize [as func] (/Users/niteshrastogi/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/commands/init/init.js:181:3) at async Command.handleAction (/Users/niteshrastogi/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/index.js:106:9)
解决步骤
1. 清理残留环境
- 删除创建失败的
AwesomeProject1文件夹 - 清理CocoaPods缓存:
pod cache clean --all - 清理npm缓存:
npm cache clean --force
2. 适配M1芯片重新安装CocoaPods
- 用Rosetta模式打开终端:右键终端→显示简介→勾选「使用Rosetta打开」
- 先卸载可能存在的brew版CocoaPods(如果之前用brew装过):
brew uninstall cocoapods - 用Ruby gem安装CocoaPods:
sudo gem install cocoapods - 初始化CocoaPods仓库(此步骤可能耗时较长,耐心等待):
pod setup
3. 手动初始化项目并安装依赖
- 创建项目时跳过自动安装依赖:
npx react-native init AwesomeProject1 --skip-install - 进入项目的ios目录:
cd AwesomeProject1/ios - 手动执行Pod安装,若仍报错则指定兼容架构:
# 普通安装 pod install # 若报错,尝试指定架构 ARCHS="arm64 x86_64" pod install - 返回项目根目录安装npm依赖:
cd .. npm install
4. 验证关键环境配置
- 检查Xcode命令行工具路径,确保指向最新Xcode:
正常输出应为xcode-select -p/Applications/Xcode.app/Contents/Developer,若不是则执行:sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer - 检查Ruby版本,React Native推荐2.7.x系列,若版本不符可通过rbenv/rvm管理:
比如用rbenv安装2.7.7:ruby -vrbenv install 2.7.7 rbenv global 2.7.7
内容的提问来源于stack exchange,提问作者Nitesh Rastogi
相关产品推荐
相关产品推荐

