执行npx react-native run-ios构建失败,但Xcode中构建成功
React Native iOS命令行构建失败(FBReactNativeSpec脚本执行错误)
原本在命令行和Xcode中均能正常构建的React Native应用,近期命令行构建突然失败,一直使用Xcode构建,现在希望修复该问题。已尝试清理缓存、执行npm install和pod install,但均未解决问题。
报错信息
The following build commands failed: PhaseScriptExecution [CP-User]\ Generate\ Specs /Users/susancrayne/Library/Developer/Xcode/DerivedData/Text911MobileApp-ftkbgymgncgaiockqfauwqqagvom/Build/Intermediates.noindex/Pods.build/Debug-iphonesimulator/FBReactNativeSpec.build/Script-337605CB7CDF90939BEBF863ED22C375.sh (in target 'FBReactNativeSpec' from project 'Pods')
Podfile内容
require_relative '../node_modules/react-native/scripts/react_native_pods' require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules' platform :ios, '11.0' target 'Text911MobileApp' do config = use_native_modules! use_react_native!( :path => config[:reactNativePath], # to enable hermes on iOS, change `false` to `true` and then install pods :hermes_enabled => false ) pod 'react-native-sms', :path => '../node_modules/react-native-sms' target 'Text911MobileAppTests' do inherit! :complete # Pods for testing end # Enables Flipper. # # Note that if you have use_frameworks! enabled, Flipper will not work and # you should disable the next line. use_flipper!() pre_install do |installer| $RNMBGL.pre_install(installer) end post_install do |installer| react_native_post_install(installer) $RNMBGL.post_install(installer) __apply_Xcode_12_5_M1_post_install_workaround(installer) installer.pods_project.build_configurations.each do |config| config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64" end installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['OTHER_CPLUSPLUSFLAGS'] = '-DDONT_AUTOINSTALL_REANIMATED' config.build_settings["CODE_SIGN_IDENTITY"] = "" end end end end
package.json内容
{ "name": "Text911MobileApp", "version": "0.0.1", "private": true, "scripts": { "android": "react-native run-android", "ios": "react-native run-ios", "start": "react-native start", "test": "jest", "lint": "eslint ." }, "dependencies": { "@react-native-async-storage/async-storage": "^1.17.4", "@react-native-community/geolocation": "^3.0.5", "@react-native-firebase/analytics": "^14.7.0", "@react-native-firebase/app": "^14.7.0", "@react-native-firebase/firestore": "^14.7.0", "@react-native-mapbox-gl/maps": "^8.5.0", "@react-navigation/native": "^6.0.2", "@react-navigation/stack": "^6.0.7", "@turf/turf": "^6.5.0", "axios": "^0.23.0", "libphonenumber-js": "^1.9.53", "react": "17.0.2", "react-native": "0.66.2", "react-native-alert-async": "^1.0.5", "react-native-bootsplash": "^4.0.0", "react-native-dialog": "^9.3.0", "react-native-egg": "^1.0.1", "react-native-gesture-handler": "^1.10.3", "react-native-keyboard-aware-scroll-view": "^0.9.5", "react-native-restart": "0.0.24", "react-native-safe-area-context": "^3.3.2", "react-native-screens": "^3.20.0", "react-native-sms": "^1.11.0", "react-native-svg": "^12.1.1" }, "devDependencies": { "@babel/core": "7.15.5", "@babel/runtime": "7.15.4", "@react-native-community/eslint-config": "2.0.0", "babel-jest": "26.6.3", "eslint": "7.14.0", "jest": "26.6.3", "metro-react-native-babel-preset": "0.66.2", "react-native-codegen": "^0.0.7", "react-test-renderer": "17.0.2" }, "jest": { "preset": "react-native" } }
可能的解决方案
1. 修复脚本执行权限
前往报错路径中的Script-337605CB7CDF90939BEBF863ED22C375.sh文件,添加执行权限:
chmod +x /Users/susancrayne/Library/Developer/Xcode/DerivedData/Text911MobileApp-ftkbgymgncgaiockqfauwqqagvom/Build/Intermediates.noindex/Pods.build/Debug-iphonesimulator/FBReactNativeSpec.build/Script-337605CB7CDF90939BEBF863ED22C375.sh
2. 彻底清理缓存与派生数据
- 清理npm缓存:
npm cache clean --force - 删除iOS目录下的Pods文件与锁:
cd ios && rm -rf Pods Podfile.lock - 删除Xcode派生数据:打开Xcode → 偏好设置 → 位置,找到
Derived Data路径,删除对应项目的文件夹 - 重新安装依赖:
npm install && cd ios && pod install --repo-update
3. 调整FBReactNativeSpec构建配置
打开iOS项目的.xcworkspace,找到Pods项目下的FBReactNativeSpec target:
- 进入
Build Phases,检查[CP-User] Generate Specs脚本的内容,确认NODE_BINARY指向正确的Node.js路径(比如nvm管理的路径:/Users/susancrayne/.nvm/versions/node/v14.18.1/bin/node) - 确认脚本是否正确调用了
react-native-codegen工具
4. 检查Node.js版本兼容性
React Native 0.66.2推荐使用Node.js 14.x版本,执行以下命令确认版本:
node -v
若版本不符,切换到14.x版本后重新构建。
5. 临时注释Podfile中的潜在冲突设置
在post_install块中,临时注释该行后重新执行pod install:
# config.build_settings['OTHER_CPLUSPLUSFLAGS'] = '-DDONT_AUTOINSTALL_REANIMATED'
内容的提问来源于stack exchange,提问作者scrayne
相关产品推荐
相关产品推荐

