Skip to content
Advertisement

Cannot destructure property ‘interface’ of ‘require(…)’ as it is undefined

I’m receiving this console error:

Cannot destructure property ‘interface’ of ‘require(…)’ as it is undefined.

Can somebody spot what is wrong?

Inbox.test.js file:

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 file: pragma solidity ^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 file:

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

Advertisement

Answer

I would suggest you verify your project structure. Your compile.js must be in a parent folder of Inbox.test.js.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement