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

运行node编译Solidity合约时报No input sources specified错误如何解决

问题原因
  • 核心错误是solc.compile方法的第一个入参填写错误:你误将solc模块本身作为编译输入传入,没有传入读取到的合约源码字符串source,导致编译器识别不到输入的Solidity源文件,抛出No input sources specified报错。
  • 额外注意:如果你的solc依赖版本为0.6.0及以上,旧的compile(源码, 优化级别)调用方式已经废弃,需要使用标准JSON输入格式调用编译器。
修复方案

适配0.4.x/0.5.x版本solc

仅需修改compile.js最后一行代码即可:

// 原错误写法
// console.log(solc.compile(solc, 1));
// 修正后写法
console.log(solc.compile(source, 1));

适配0.6.x及以上版本solc

新版本编译器要求使用标准JSON输入格式,完整代码如下:

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

const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8');

// 构造标准编译输入
const compileInput = {
  language: 'Solidity',
  sources: {
    'Inbox.sol': {
      content: source
    }
  },
  settings: {
    outputSelection: {
      '*': {
        '*': ['*']
      }
    }
  }
};

// 执行编译并解析输出
const compileOutput = JSON.parse(solc.compile(JSON.stringify(compileInput)));
console.log(compileOutput);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 23:42:02