无法解构 'require(...)' 的 属性 'interface',因为它未定义

Cannot destructure property 'interface' of 'require(...)' as it is undefined

我收到此控制台错误:“无法解构 'require(...)' 的 属性 'interface',因为它未定义。”

有人能指出错误吗?

Inbox.test.js 文件:

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const {interface, bytecode} = require('../compile');

let accounts;
let inbox;

beforeEach(async ()=>{
// get a list of all accounts.
accounts = await web3.eth.getAccounts();
    // use ne of them to deploy.
inbox = await new web3.eth.Contract(JSON.parse(interface))
.deploy({data: bytecode, arguments: ['Hi there!'] })
.send({from: accounts[0], gas: '1000000'});
});

describe('Inbox', ()=>{
it('deploys a contract', ()=>{
    console.log(inbox);
});
});

inbox.sol 文件: 实用性 ^0.4.17;

contract Inbox{
string public message;
function inbox(string initialMessage) public {
    message = initialMessage;
}
function setMessage(string newMessage) public {
    message = newMessage;
}
function doMath(int a, int b){
    a+b;
    b-a;
    b*a;
    a==0;
}
}

compile.js 文件:

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');

module.exports = solc.compile(source, 1).contracts[':Inbox'];

我建议您验证您的项目结构。您的 compile.js 必须位于 Inbox.test.js 的父文件夹中。

问题实际上出在你的 compile.js 文件上 尝试 运行 它使用节点 compile.js 你会发现错误。

这只是因为 compile.js 由于 inbox.sol 文件中的某些错误而返回未定义。

要深入挖掘,您可以尝试 运行 您的 inbox.sol 文件。