React Native Expo iOS Release构建报错:Cannot read property 'transformFile' of undefined
通过Xcode或Expo CLI构建iOS应用Release版本时触发报错:Cannot read property 'transformFile' of undefined,但Debug模式下能在模拟器正常运行。
项目信息
- React Native v0.73.4
- Node v18.17
- Expo v50(裸工作流)
已尝试的解决方案
- 删除
package-lock.json、node_modules、pods、podfile.lock后重新安装 - 删除并重建iOS构建文件夹
- 将Node版本降级至v18最低版本
- 终端执行
export NODE_OPTIONS=--openssl-legacy-provider - 执行
npx expo install --fix(新项目无修复内容)
堆栈跟踪
error: Cannot read property 'transformFile' of undefined TypeError: Cannot read property 'transformFile' of undefined TypeError: Cannot read property 'transformFile' of undefined at Bundler.transformFile (/Users/[USER]/Documents/Projects/sentiance-expo/node_modules/metro/src/Bundler.js:43:30) at async Object.transform (/Users/[USER]/Documents/Projects/sentiance-expo/node_modules/metro/src/lib/transformHelpers.js:130:12) at async transform (/Users/[USER]/Documents/Projects/sentiance-expo/node_modules/metro/src/DeltaBundler/Graph.js:164:26) at async visit (/Users/[USER]/Documents/Projects/sentiance-expo/node_modules/metro/src/DeltaBundler/buildSubgraph.js:82:29) at async Promise.all (index 0) at async buildSubgraph (/Users/[USER]/Documents/Projects/sentiance-expo/node_modules/metro/src/DeltaBundler/buildSubgraph.js:103:3) at async Graph._buildDelta (/Users/[USER]/Documents/Projects/sentiance-expo/node_modules/metro/src/DeltaBundler/Graph.js:157:22) at async Graph.initialTraverseDependencies (/Users/[USER]/Documents/Projects/sentiance-expo/node_modules/metro/src/DeltaBundler/Graph.js:140:19) at async DeltaCalculator._getChangedDependencies (/Users/[USER]/Documents/Projects/sentiance-expo/node_modules/metro/src/DeltaBundler/DeltaCalculator.js:151:25) at async DeltaCalculator.getDelta (/Users/[USER]/Documents/Projects/sentiance-expo/node_modules/metro/src/DeltaBundler/DeltaCalculator.js:68:16)
系统信息
OS: macOS 13.5.2 CPU: (10) arm64 Apple M1 Pro Memory: 151.78 MB / 16.00 GB Shell: version: "5.9" path: /bin/zsh Binaries: Node: version: 18.17.0 path: ~/.nvm/versions/node/v18.17.0/bin/node Yarn: version: 1.22.15 path: ~/.yarn/bin/yarn npm: version: 9.6.7 path: ~/.nvm/versions/node/v18.17.0/bin/npm Watchman: version: 2024.01.22.00 path: /opt/homebrew/bin/watchman Managers: CocoaPods: version: 1.15.2 path: /opt/homebrew/bin/pod SDKs: iOS SDK: Platforms: - DriverKit 23.0 - iOS 17.0 - macOS 14.0 - tvOS 17.0 - watchOS 10.0 Android SDK: Not Found IDEs: Android Studio: 2023.1 AI-231.9392.1.2311.11330709 Xcode: version: 15.0/15A240d path: /usr/bin/xcodebuild Languages: Java: version: 17.0.10 path: /usr/bin/javac Ruby: version: 2.6.10 path: /usr/bin/ruby npmPackages: "@react-native-community/cli": Not Found react: installed: 18.2.0 wanted: 18.2.0 react-native: installed: 0.73.4 wanted: 0.73.4 react-native-macos: Not Found npmGlobalPackages: "*react-native*": Not Found Android: hermesEnabled: true newArchEnabled: false iOS: hermesEnabled: true newArchEnabled: false
可能的解决方案
校验Babel与Metro版本兼容性
这个报错核心是Metro无法找到正确的Babel转换函数,先检查babel.config.js配置是否符合React Native 0.73.4的要求,再执行npm list @babel/core查看版本,若版本不兼容,安装匹配版本:npm install @babel/core@^7.20.0 --save-dev临时禁用Hermes排查冲突
当前iOS启用了Hermes,尝试在ios/Podfile中关闭Hermes,重新安装pods后构建Release版本:use_react_native!( :path => config[:reactNativePath], # 注释或删除此行 # :hermes_enabled => true )执行
cd ios && pod install后重新构建,若正常则说明Hermes与项目依赖存在冲突,可尝试升级Hermes版本或逐一排查第三方依赖。清理Metro缓存
执行命令彻底清理Metro缓存:npx metro-cache clean清理完成后重新构建Release版本。
检查Xcode构建脚本参数
打开Xcode的Build Phases,查看Bundle React Native code and images的脚本参数,确认Release模式下是否指定了正确的Metro配置文件(比如添加--config metro.config.js),避免参数错误导致Metro初始化异常。重新生成Expo裸工作流原生文件
执行以下命令重新生成iOS原生文件,确保Expo配置与React Native版本匹配:npx expo prebuild --clean完成后重新安装pods并构建。
内容的提问来源于stack exchange,提问作者cyanbird

