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

React Native中http模块无法解析错误的解决求助

解决React Native中无法解析http模块的问题

针对你遇到的无法从/node_modules/https-browserify/index.js解析http模块错误,尝试以下针对性方案:

  • 安装浏览器端模块的polyfill包
    执行以下命令安装缺失的依赖:

    yarn add http-browserify https-browserify stream-browserify --dev
    

    若使用npm则执行:

    npm install http-browserify https-browserify stream-browserify --save-dev
    
  • 配置Metro解析器映射模块
    修改项目根目录下的metro.config.js文件,添加模块映射规则:

    const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
    
    module.exports = mergeConfig(getDefaultConfig(__dirname), {
      resolver: {
        extraNodeModules: {
          http: require.resolve('http-browserify'),
          https: require.resolve('https-browserify'),
          stream: require.resolve('stream-browserify'),
        },
      },
    });
    
  • 调整Metro的主字段解析顺序(可选)
    若上述步骤无效,可能是部分库的browser字段未被正确识别,可在metro.config.js的resolver中补充配置:

    resolver: {
      mainFields: ['browser', 'module', 'main'],
    }
    
  • 排查依赖库兼容性
    检查项目中是否引入了仅支持浏览器环境的第三方库,替换为React Native兼容的替代版本(比如使用React Native原生的网络API,或选择有RN适配的库)

完成配置后,重新执行缓存重置与项目启动:

yarn start --reset-cache
yarn run android

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 06:20:22