'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 the 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'];
Solution 1:[1]
I would suggest you verify your project structure. Your compile.js must be in a parent folder of Inbox.test.js.
Solution 2:[2]
The problem actually lies in your compile.js file try running it using node compile.js you will find the error.
This is just because compile.js is returning undefined due to some error in the inbox.sol file.
For digging deeper you can try running your inbox.sol file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | d3mage |
| Solution 2 | Mastermind |
