Xcode14.2归档React Native项目时main.jsbundle.map缺失报错求助
React Native 归档时找不到临时目录下的main.jsbundle.map 问题解决
问题描述
使用Xcode14.2通过「Product」>「Archive」功能归档React Native项目时,触发以下错误:
export WRAPPER_SUFFIX\=.app export WRAP_ASSET_PACKS_IN_SEPARATE_DIRECTORIES\=NO export XCODE_APP_SUPPORT_DIR\=/Applications/Xcode.app/Contents/Developer/Library/Xcode export XCODE_PRODUCT_BUILD_VERSION\=14C18 export XCODE_VERSION_ACTUAL\=1420 export XCODE_VERSION_MAJOR\=1400 export XCODE_VERSION_MINOR\=1420 export XPCSERVICES_FOLDER_PATH\=PickupAppV2.app/XPCServices export YACC\=yacc export _WRAPPER_CONTENTS_DIR_SHALLOW_BUNDLE_NO\=/Contents export _WRAPPER_PARENT_PATH_SHALLOW_BUNDLE_NO\=/.. export _WRAPPER_RESOURCES_DIR_SHALLOW_BUNDLE_NO\=/Resources export __IS_NOT_MACOS\=YES export __IS_NOT_MACOS_macosx\=NO export __IS_NOT_SIMULATOR\=YES export __IS_NOT_SIMULATOR_simulator\=NO export arch\=undefined_arch export variant\=normal /bin/sh -c /Users/jamesku/Library/Developer/Xcode/DerivedData/PickupAppV2-bdtpzdicjgbodpdojnmikcdhwrmk/Build/Intermediates.noindex/ArchiveIntermediates/PickupAppV2-DEV/IntermediateBuildFilesPath/PickupAppV2.build/Release-iphoneos/PickupAppV2.build/Script-023B83D0DA214377BFA53CD9.sh Error: SOURCE_MAP /var/folders/bx/fdwrm4kd7fn6h7kylslgj1340000gn/T//4e26d60a5d37aa8a4edb6ec522927ef6-main.jsbundle.map could not be found. Ensure the --sourcemap-output option is passed to the react-native bundle command. Command PhaseScriptExecution failed with a nonzero exit code
已执行的操作:
- 按照React Native官方文档启用源映射
- 手动执行命令生成映射文件并添加到项目:
NODE_OPTIONS=--openssl-legacy-provider react-native bundle --entry-file='index.ios.js' --bundle-output='./ios/main.jsbundle' --dev=false --platform='ios' --sourcemap-output ios/main.jsbundle.map
解决方案
1. 修正Xcode中的Bundle脚本参数
Xcode归档时调用的自动脚本(即报错中的Script-xxxx.sh)可能未正确配置源映射输出参数:
- 打开Xcode项目,进入
Build Phases面板,找到Bundle React Native Code And Images - 查看脚本内容,确保bundle命令包含
--sourcemap-output参数,指向Xcode构建时的临时目录,示例修改后的脚本:
export NODE_BINARY=node ../node_modules/react-native/scripts/react-native-xcode.sh --sourcemap-output "$CONFIGURATION_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH/main.jsbundle.map"
2. 清理全量缓存后重试
- 清理React Native缓存:
watchman watch-del-all && rm -rf node_modules && rm -rf package-lock.json && npm install && rm -rf $TMPDIR/react-native-packager-cache-* && rm -rf $TMPDIR/metro-bundler-cache-*
- 清理Xcode缓存:点击
Product>Clean Build Folder(快捷键Cmd+Shift+K),手动删除报错信息中指定的DerivedData目录 - 重新执行归档操作
3. 用绝对路径生成源映射文件
手动生成映射时使用绝对路径,避免相对路径导致Xcode无法定位:
NODE_OPTIONS=--openssl-legacy-provider react-native bundle --entry-file='/Users/jamesku/Projects/PickupAppV2/index.ios.js' --bundle-output='/Users/jamesku/Projects/PickupAppV2/ios/main.jsbundle' --dev=false --platform='ios' --sourcemap-output '/Users/jamesku/Projects/PickupAppV2/ios/main.jsbundle.map'
生成后,确认main.jsbundle.map已添加到Xcode项目的Copy Bundle Resources列表中。
4. 检查RN版本与Xcode兼容性
Xcode14.2对旧版React Native存在兼容性问题,若使用RN版本低于0.70,建议升级到官方标注兼容Xcode14的版本。
内容的提问来源于stack exchange,提问作者james ku
相关产品推荐
相关产品推荐

