You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac M1运行React Native iOS项目时React-Codegen报错求助

解决React Native 0.74.3在M1 Mac上运行iOS模拟器时React-Codegen脚本执行错误

问题重现

执行命令:

npx react-native run-ios --simulator "iPhone 15 Pro"

触发错误:

PhaseScriptExecution [CP-User]\ Generate\ Specs /Users/databasket/Library/Developer/Xcode/DerivedData/DbAttendance-azvmhjqlipxdpkgernpvlryirpgy/Build/Intermediates.noindex/Pods.build/Debug-iphonesimulator/React-Codegen.build/Script-46EB2E00026CE0.sh (in target 'React-Codegen' from project 'Pods')

环境配置:

  • React Native 0.74.3、React 18.2.0
  • Ruby 2.6.10、CocoaPods 1.15.2
  • Node 21.7.3
  • Xcode 15.4、macOS 14.4.1、iOS 17.5

已尝试常规清理步骤(删除DerivedData、Podfile.lock、Pods、node_modules,重新执行yarn install、pod install、run-ios)无效。

解决步骤

1. 确保Xcode命令行工具与Xcode版本匹配

  • 打开Xcode → 偏好设置 → Locations,确认Command Line Tools选择的是Xcode 15.4。
  • 终端执行以下命令验证路径:
    xcode-select -p
    
    若输出不是/Applications/Xcode.app/Contents/Developer,执行以下命令切换:
    sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
    

2. 调整React-Codegen脚本权限并尝试Rosetta运行终端

  • 进入项目的ios/Pods/React-Codegen目录,找到报错的脚本文件(如Script-46EB2E00026CE0.sh),赋予执行权限:
    chmod +x Script-46EB2E00026CE0.sh
    
  • M1芯片可能存在架构兼容问题,右键终端图标 → 选择"使用Rosetta打开",重新执行pod install和npx react-native run-ios。

3. 切换Node.js到LTS版本

React Native 0.74.3推荐使用Node.js 18.x或20.x LTS版本(当前使用的21.x为非LTS版本,可能存在兼容性问题):

  • 使用nvm切换版本(已安装nvm的情况下):
    nvm install 18
    nvm use 18
    
  • 重新执行依赖安装和构建:
    rm -rf node_modules && yarn install
    cd ios && pod deintegrate && pod install && cd ..
    npx react-native run-ios --simulator "iPhone 15 Pro"
    

4. 手动在Xcode中构建并排查详细错误

  • 打开项目的ios/DbAttendance.xcworkspace文件(替换为你的项目名)。
  • 选择iPhone 15 Pro模拟器,先清理项目(Command + Shift + K),再执行构建(Command + B)。
  • 查看Xcode控制台输出的详细错误信息,若脚本执行失败,可进入Targets → Pods → React-Codegen → Build Phases,检查[CP-User] Generate Specs脚本中的Node路径是否正确,确保指向已切换的LTS版本。

5. 彻底重置CocoaPods配置

  • 执行以下命令彻底清理并重新初始化Pods:
    cd ios
    pod deintegrate
    rm -rf Podfile.lock Pods
    pod setup
    pod install
    cd ..
    
  • 重新运行项目:
    npx react-native run-ios --simulator "iPhone 15 Pro"
    

内容的提问来源于stack exchange,提问作者Shyam Laishram

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 12:12:24