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

React-Native钱包集成WalletConnect V1遇crypto模块解析失败求助

解决React-Native集成WalletConnect V1时的crypto模块缺失问题

你遇到的问题是因为WalletConnect V1的依赖(@walletconnect/randombytes)调用了Node.js原生的crypto模块,但React-Native环境中并没有这个模块。装crypto-js没用是因为它和Node的cryptoAPI不兼容,而react-native-crypto是专门适配RN的替代方案,但需要正确配置才能生效,按以下步骤操作:

  • 补全必要依赖并完成链接:

    npm install react-native-crypto react-native-randombytes @react-native-community/netinfo buffer readable-stream
    npx react-native link react-native-randombytes
    
  • 修改metro.config.js,配置Metro解析器将Node的crypto等模块替换为RN兼容版本:

    const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
    
    module.exports = mergeConfig(getDefaultConfig(__dirname), {
      resolver: {
        extraNodeModules: {
          crypto: require.resolve('react-native-crypto'),
          stream: require.resolve('readable-stream'),
          buffer: require.resolve('buffer'),
        },
      },
    });
    
  • 在项目入口文件(比如index.js或App.js)顶部添加全局变量声明,补全Node环境的全局对象:

    import { Buffer } from 'buffer';
    global.Buffer = Buffer;
    global.process = {
      env: { ...process.env },
      version: '',
    };
    
  • 清理缓存并重启服务:

    npx react-native start --reset-cache
    # 之后重新运行iOS/Android项目
    npx react-native run-ios
    # 或者
    npx react-native run-android
    

完成这些步骤后,WalletConnect应该能正确找到crypto模块,解决导入报错的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 05:05:14