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

React Native项目Metro打包持续失败:无法解析@babel/runtime依赖

解决Metro打包器无法解析@babel/runtime/helpers/interopRequireDefault的问题

核心解决方案步骤:

  • 安装缺失的@babel/runtime依赖:
    执行命令:
    npm install @babel/runtime --save
    # 或使用yarn
    yarn add @babel/runtime
    
  • 强制清除Metro缓存,解决缓存导致的解析异常:
    # React Native原生项目
    npx react-native start --reset-cache
    # Expo项目
    expo start -c
    
  • 验证Babel配置正确性,确保babel.config.js使用正确的预设:
    Expo项目配置示例:
    module.exports = function(api) {
      api.cache(true);
      return {
        presets: ['babel-preset-expo'],
      };
    };
    
    React Native原生项目配置示例:
    module.exports = function(api) {
      api.cache(true);
      return {
        presets: ['module:metro-react-native-babel-preset'],
      };
    };
    
  • 彻底重装依赖,解决版本冲突:
    # 删除依赖目录与锁文件(Windows替换rm为rmdir /s /q,del替换rm)
    rm -rf node_modules package-lock.json yarn.lock
    # 重新安装
    npm install
    # 或yarn install
    
  • 检查Node.js版本,建议使用LTS版本(如16.x、18.x),避免版本不兼容导致的依赖问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 10:30:55