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

Expo项目Metro配置blockList不生效,报Transformer不是构造函数错误

解决方案

错误根源

你的配置存在两个核心问题:

  1. 直接丢弃了getDefaultConfig返回的默认transformer完整配置,仅自定义了少量参数,导致Metro缺少初始化转换器的必要属性,触发Transformer is not a constructor报错。
  2. blockList的正则匹配规则范围过大,会误伤node_modules中的官方Amplify依赖文件,反而会导致正常功能异常。

修复步骤

  • 替换为正确的合并式Metro配置,使用Expo官方提供的mergeConfig能力合并默认配置和自定义规则,避免缺省参数:
const { getDefaultConfig, mergeConfig } = require("expo/metro-config");

module.exports = (async () => {
  const defaultConfig = await getDefaultConfig(__dirname);
  const customConfig = {
    transformer: {
      babelTransformerPath: require.resolve("react-native-svg-transformer"),
      assetPlugins: ["expo-asset/tools/hashAssetFiles"],
    },
    resolver: {
      assetExts: defaultConfig.resolver.assetExts.filter(ext => ext !== "svg"),
      sourceExts: [...defaultConfig.resolver.sourceExts, "svg"],
      // 仅匹配项目根目录的amplify文件夹,不影响官方Amplify依赖
      blockList: [...defaultConfig.resolver.blockList, /\/amplify\//]
    }
  };
  return mergeConfig(defaultConfig, customConfig);
})();
  • 检查依赖版本适配:Expo SDK 48及以上版本需要配套使用react-native-svg-transformer@1.0.0+,同时确保你已经安装了对应版本的react-native-svg依赖。
  • 清理缓存生效:修改配置后执行npx expo start --clear启动项目,清除Metro旧缓存避免配置不生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 09:18:03