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

运行node compile.js报ERR_ASSERTION无效回调对象错误如何解决?

问题排查与解决方案

错误原因

  • 合约构造函数语法不符合0.8.x版本Solidity规范:与合约同名的函数作为构造函数是0.4.22之前的废弃写法,同时原代码存在参数拼写错误、未指定引用类型数据位置的问题。
  • solc编译调用方式不兼容:你安装的0.8.9版本solc已废弃旧版本的回调式编译API,你当前compile.js中使用的0.4.x版本适配的编译逻辑会触发该断言错误。

修复步骤

1. 修正Inbox.sol合约代码

pragma solidity ^0.8.9;

contract Inbox{
    string public message;

    // 改用constructor关键字声明构造函数,明确指定string参数的memory存储位置,修正拼写错误
    constructor(string memory initialMessage) {
        message = initialMessage;
    }

    function setMessage(string memory newMessage) public {
        message = newMessage;
    }
}

2. 修正compile.js编译逻辑

改用solc 0.5+版本支持的标准JSON输入编译方式,示例代码如下:

const path = require('path');
const fs = require('fs');
const solc = require('solc');

// 此处路径修改为你本地Inbox.sol的实际存放路径
const inboxPath = path.resolve(__dirname, 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8');

const input = {
  language: 'Solidity',
  sources: {
    'Inbox.sol': {
      content: source
    }
  },
  settings: {
    outputSelection: {
      '*': {
        '*': ['*']
      }
    }
  }
};

// 编译后导出合约的ABI和字节码
const compileResult = JSON.parse(solc.compile(JSON.stringify(input)));
module.exports = compileResult.contracts['Inbox.sol'].Inbox;

3. 验证

重新执行node compile.js即可完成正常编译,不会再抛出该错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 16:24:07