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:
JavaScript
x
24
24
1
const assert = require('assert');
2
const ganache = require('ganache-cli');
3
const Web3 = require('web3');
4
const web3 = new Web3(ganache.provider());
5
const {interface, bytecode} = require('../compile');
6
7
let accounts;
8
let inbox;
9
10
beforeEach(async ()=>{
11
// get a list of all accounts.
12
accounts = await web3.eth.getAccounts();
13
// use ne of them to deploy.
14
inbox = await new web3.eth.Contract(JSON.parse(interface))
15
.deploy({data: bytecode, arguments: ['Hi there!'] })
16
.send({from: accounts[0], gas: '1000000'});
17
});
18
19
describe('Inbox', ()=>{
20
it('deploys a contract', ()=>{
21
console.log(inbox);
22
});
23
});
24
inbox.sol file: pragma solidity ^0.4.17;
JavaScript
1
16
16
1
contract Inbox{
2
string public message;
3
function inbox(string initialMessage) public {
4
message = initialMessage;
5
}
6
function setMessage(string newMessage) public {
7
message = newMessage;
8
}
9
function doMath(int a, int b){
10
a+b;
11
b-a;
12
b*a;
13
a==0;
14
}
15
}
16
compile.js file:
JavaScript
1
9
1
const path = require('path');
2
const fs = require('fs');
3
const solc = require('solc');
4
5
const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
6
const source = fs.readFileSync(inboxPath,'utf8');
7
8
module.exports = solc.compile(source, 1).contracts[':Inbox'];
9
Advertisement
Answer
I would suggest you verify your project structure. Your compile.js
must be in a parent folder of Inbox.test.js
.