That's the error that occurs when I try to test my contract.

Can someone tell me what the problem is?

That's the code for the test:

const assert = require('assert');
const fs = require('fs');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());

const compiledFactory = fs.readFileSync('./ethereum/build/LotteriaFactory.json', 'utf8');
const compiledLotteria = fs.readFileSync('./ethereum/build/Lotteria.json', 'utf8');

let accounts;
let factory;
let lotteriaAddress;
let lotteria;

beforeEach(async () => {
  accounts = await web3.eth.getAccounts();

  factory = await new web3.eth.Contract(JSON.parse(compiledFactory).interface)
  .deploy({ data: (compiledFactory).bytecode })
  .send({ from: accounts[0], gas: '1000000' });

  await factory.methods.createNew('5').send({
    from: accounts[0], //manager
    gas: '1000000'

  const addresses = await factory.methods.active().call();
  lotteriaAddress = addresses[0]; //[LotteriaFactory]

   lotteria = new web3.eth.Contract(

describe('Lotteria', () => {
  it('deploy a factory and a lottery', () => {
  • 2
    Can you please console.log(typeof JSON.parse(compiledFactory).interface) and tell us what you get? Commented Mar 18, 2020 at 13:56
  • Alright so I tryed and I get "undefined"
    – Gianna
    Commented Mar 18, 2020 at 15:10
  • 1
    This means that you have no interface field in the JSON object which the string compiledFactory is parsed into. Print it (or simply open the file which you read it from), and find out what fields you have there, and which one of them represents a legal ABI. Commented Mar 18, 2020 at 15:14
  • 1
    I tryed with JSON.parse(compiledFactory).abi and that seems to be right (you was right, I don't have any Interface property but instead I've got ABI). The problem is that when I test I got another Error:"cannot read property 'send' of null". What does this mean?
    – Gianna
    Commented Mar 18, 2020 at 15:19
  • 1
    Well, that's a different question than the one asked here (about "Error: you must provide the json interface of the contract when instantiating a contract object"). I suggest that you first debug this problem for a while, and then post it as a separate question, including all relevant information from your debug session (i.e., don't just post every error that you get along the way). Commented Mar 18, 2020 at 15:40

2 Answers 2


You could instead try something like this to instantiate a contract :

  • copy paste the abi of your contract in a json file

  • read the file : const contractJson = fs.readFileSync('path_to_abi_file.json');

  • parse the file : const abi = JSON.parse(contractJson);

  • instantiate the contract : contractInstance = new web3.eth.Contract(abi);

  • 3
    You just made up a file named path_to_abi_file.json, which you want to contain a proper ABI, but that's exactly the problem that this user is dealing with in her input file. If she knew how to create such file, then she wouldn't have this problem to begin with. Her input file obviously has a different format (some other object, with a field named interface). So I doubt that your answer could contribute much. Commented Mar 18, 2020 at 14:52
  • 1
    It's clearly a bloke...
    – Rambatino
    Commented Jun 4, 2021 at 19:03

Here you have another example to load contracts with no require function. Working on Web3.js v4 2024

const loadContracts = async () => {
            const networkID = await web3.eth.net.getId();
            const getContractData = async (contract_path) => {
                const contractJson = await (await fetch(contract_path)).json();
                const abi = await contractJson.abi;
                const contract_address = contractJson.networks[networkID].address;
                return new web3.eth.Contract(abi, contract_address)
      const myLoadedContract = await getContractData("PATH_TO_YOUR_CONTRACT.JSON")
      // Everything else..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.