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

Expo Bare Workflow iOS打包失败:Failed to construct transformer错误

Expo Bare Workflow iOS Release构建失败:SyntaxError: Unexpected token '??='

问题概述

构建Expo 50 Bare Workflow iOS Release版本时(执行npx expo run:ios --configuration Release或通过Xcode构建),出现以下错误:

Failed to construct transformer:  /Users/{USER}/Documents/Projects/sentiance-expo/node_modules/@expo/metro-config/build/transform-worker/asset-transformer.js:21
    options ??= options || {
            ^^^

SyntaxError: Unexpected token '??='

伴随栈跟踪:

error: 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)

应用在Android和iOS模拟器Dev模式下可正常运行,已尝试清理依赖、降级Node至v18、expo-doctor等操作,问题未解决。

环境核心配置:

  • Expo: ^50.0.0
  • React Native: 0.73.4
  • Node: v18.x
  • babel.config.js: 使用babel-preset-expo和react-native-reanimated/plugin

解决方案

1. 确保Xcode使用的Node版本支持ES2020

??=是ES2020语法,Node v14+已支持,但Xcode可能默认调用系统自带的旧版本Node:

  • 打开Xcode,进入Preferences > Locations,确认Command Line Tools选择的是与终端一致的版本。
  • 进入Xcode项目的Build Phases > Bundle React Native code and images,修改脚本开头的Node路径为当前使用的版本路径(比如nvm安装的路径):
    # 替换为你的Node实际路径,示例为nvm安装的v18.19.0
    export NODE_BINARY="$HOME/.nvm/versions/node/v18.19.0/bin/node"
    ../node_modules/react-native/scripts/react-native-xcode.sh
    

2. 配置Metro强制转译@expo/metro-config

默认Metro可能跳过转译node_modules中的@expo/metro-config,导致未转译的ES2020语法出错。修改metro.config.js:

const { getDefaultConfig } = require('@expo/metro-config');
const path = require('path');

const config = getDefaultConfig(__dirname);

// 强制转译@expo/metro-config模块
config.resolver.resolveRequest = (context, moduleName, platform) => {
  if (moduleName.startsWith('@expo/metro-config')) {
    return {
      filePath: path.resolve(__dirname, `node_modules/${moduleName}`),
      type: 'sourceFile',
    };
  }
  return context.resolveRequest(context, moduleName, platform);
};

// 确保使用babel-preset-expo转译
config.transformer.transformWithBabelPath = require.resolve('babel-preset-expo');

module.exports = config;

3. 补充Babel插件强化ES2020支持

虽然babel-preset-expo已包含相关插件,但手动添加可确保兼容性:
修改babel.config.js:

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ["babel-preset-expo"],
    plugins: [
      "react-native-reanimated/plugin",
      "@babel/plugin-proposal-nullish-coalescing-operator",
      "@babel/plugin-proposal-logical-assignment-operators"
    ],
  };
};

4. 彻底清理缓存并重新构建

执行以下命令清理所有缓存后重新构建:

# 清理Metro缓存
npx metro-cache clean
# 清理依赖和iOS构建文件
rm -rf node_modules package-lock.json ios/Pods ios/Podfile.lock ios/build
npm install
cd ios && pod install && cd ..
# 重新构建Release版本
npx expo run:ios --configuration Release

5. 确认Hermes启用状态

RN 0.73默认启用Hermes,它对ES2020语法支持更完善,检查配置:

  • 查看ios/Podfile中是否有use_react_native!(..., :hermes_enabled => true)
  • 检查Xcode项目Build Settings中ENABLE_HERMES是否设为YES

内容的提问来源于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 22:50:54