执行node compile.js触发Solidity编译栈溢出错误求助
解决Solidity编译时的RangeError: Maximum call stack size exceeded问题
我遇到过一模一样的问题,结合你的代码和环境,下面是几个可行的解决方案:
1. 先修正compile.js里的导出语法错误
你的代码里把module.exports.compile写成了module.compile,虽然这不一定是栈溢出的直接原因,但会导致后续无法正确引用编译结果,先把这个小问题修正:
const path = require('path'); const fs = require('fs'); const solc = require('solc'); const invoxPath = path.resolve(__dirname, 'contracts', 'invox.sol'); const source = fs.readFileSync(invoxPath, 'utf8'); // 修正导出语法 module.exports.compile = solc.compile(source, 1).contracts[':Invox'];
2. 降级Node.js到兼容版本
旧版本的solc@0.4.25和Node.js 16及以上版本存在兼容性问题,会触发调用栈溢出错误。你可以用nvm来快速管理Node.js版本:
- 安装nvm后,执行
nvm install 14 - 切换到14版本:
nvm use 14 - 清理旧依赖重新安装:
rm -rf node_modules package-lock.json && npm install - 再次运行
node compile.js试试
3. 更换solc版本或改用标准输入输出调用方式
如果不想降级Node.js,可以试试这两个方案:
方案A:升级solc到同大版本的最新版
修改package.json里的solc依赖:
{ "dependencies": { "solc": "^0.4.26" } }
然后重新安装依赖并运行编译。
方案B:使用solc标准输入输出格式调用
这种调用方式更稳定,也兼容新版本Node.js,修改compile.js为:
const path = require('path'); const fs = require('fs'); const solc = require('solc'); const invoxPath = path.resolve(__dirname, 'contracts', 'invox.sol'); const source = fs.readFileSync(invoxPath, 'utf8'); // 构建标准输入对象 const input = { language: 'Solidity', sources: { 'Invox.sol': { content: source } }, settings: { outputSelection: { '*': { '*': ['*'] } } } }; // 编译并提取结果 const output = JSON.parse(solc.compile(JSON.stringify(input))); module.exports.compile = output.contracts['Invox.sol'].Invox;
额外检查:合约文件路径和命名
确认你的合约文件确实放在contracts/invox.sol(注意小写),和代码里的invoxPath路径完全匹配,大小写不匹配也可能引发隐性问题。
内容的提问来源于stack exchange,提问作者Fernando López
相关产品推荐
相关产品推荐

