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

React Native 0.70.x升级ethers v5.7至v6.7后出现私有方法编译错误

React Native 0.70.x升级ethers至v6.7后私有方法编译错误解决办法

升级ethers到v6.7后出现以下编译错误:

error: node_modules/ethers/lib.commonjs/abi/abi-coder.js: /Users/macair/Documents/code/js/xyz_app6/node_modules/ethers/lib.commonjs/abi/abi-coder.js: Class private methods are not enabled. Please add `@babel/plugin-proposal-private-methods` to your configuration.
  111 |  */
  112 | class AbiCoder {
> 113 |     #getCoder(param) {
      |     ^
  114 |         if (param.isArray()) {
  115 |             return new array_js_1.ArrayCoder(this.#getCoder(param.arrayChildren), param.arrayLength, param.name);
  116 |         }
 BUNDLE  ./index.js 

已尝试添加babel插件但未解决,可按以下步骤排查修复:

  • 修正babel配置的模块匹配规则
    原配置中使用相对路径./node_modules/ethers无法正确匹配模块,需改为正则表达式。同时确保插件名称与错误提示一致,修改babel.config.js:

    module.exports = {
      // 保留原有配置...
      overrides: [
        {
          test: /node_modules\/ethers/,
          plugins: [["@babel/plugin-proposal-private-methods", { loose: true }]]
        }
      ]
    };
    
  • 配置Metro转译ethers模块
    React Native默认不会转译node_modules中的第三方库,需在metro.config.js中添加ethers到转译列表:

    const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
    
    module.exports = mergeConfig(getDefaultConfig(__dirname), {
      transformer: {
        transformNodeModules: ['ethers'],
        getTransformOptions: async () => ({
          transform: {
            experimentalImportSupport: false,
            inlineRequires: true,
          },
        }),
      },
    });
    
  • 清理缓存并重新编译
    执行以下命令清除Metro缓存,避免旧编译结果干扰:

    npx react-native start --reset-cache
    

    之后重新运行项目即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 05:32:29