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

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

可能的解决方案

  1. 校验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
    
  2. 临时禁用Hermes排查冲突
    当前iOS启用了Hermes,尝试在ios/Podfile中关闭Hermes,重新安装pods后构建Release版本:

    use_react_native!(
      :path => config[:reactNativePath],
      # 注释或删除此行
      # :hermes_enabled => true
    )
    

    执行cd ios && pod install后重新构建,若正常则说明Hermes与项目依赖存在冲突,可尝试升级Hermes版本或逐一排查第三方依赖。

  3. 清理Metro缓存
    执行命令彻底清理Metro缓存:

    npx metro-cache clean
    

    清理完成后重新构建Release版本。

  4. 检查Xcode构建脚本参数
    打开Xcode的Build Phases,查看Bundle React Native code and images的脚本参数,确认Release模式下是否指定了正确的Metro配置文件(比如添加--config metro.config.js),避免参数错误导致Metro初始化异常。

  5. 重新生成Expo裸工作流原生文件
    执行以下命令重新生成iOS原生文件,确保Expo配置与React Native版本匹配:

    npx expo prebuild --clean
    

    完成后重新安装pods并构建。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 19:17:05