My NodeJS script gives me this error:

1)"before each" hook for "deploy a factory and a lottery"
SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse((anonymous))
at Context.(anonymous) (test/lottery/test.js:18:46)
at processTicksAndRejections(internal/process/task_queues.js:94:5)

File test.js

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

const compiledFactory = require('../ethereum/build/LotteriaFactory.json');
const compiledLotteria = require('../ethereum/build/Lotteria.json');
//two istances of the contract inside the build dir

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('Lottery', () => {
  it('deploy a factory and a lottery', () => {

My dependencies are:

  • solc 0.5.0
  • web3 1.2.6

Can someone help me? What I am missing?

  • That's not an "npm error", but a NodeJS error (more specifically, an error from the Mocha testing framework). Mar 17, 2020 at 21:31

1 Answer 1


Your problem is the attempt to JSON.parse(compiledFactory.interface).

Function JSON.parse expects an input string which represents a valid JSON object.

You're attempting to obtain that object by requiring a JSON file:

const compiledFactory = require('../ethereum/build/LotteriaFactory.json');

But require statements are designated for importing JS code, not JSON objects!

Instead of requiring this file, you should simply read it:

const fs = require('fs');
const compiledFactory = fs.readFileSync('../ethereum/build/LotteriaFactory.json', {encoding: "utf8"});

Finally, in order to access the interface field of that object, change this:


To this:

  • Thank you very much for the answer, it was really helpfull to undersrand how JSON.parse works! Now i'm facing an other problem, I've changed the code but npm returns ("Error: you must provide the json interface of the contract when initiating a contract object" My code right now is: factory = await new web3.eth.Contract(JSON.parse(compiledFactory).interface) .deploy({ data: (compiledFactory).bytecode }) .send({ from: accounts[0], gas: '1000000' }); And the compiled object is: const compiledFactory = fs.readFileSync('./ethereum/build/LotteriaFactory.json', 'utf8');
    – Gianna
    Mar 18, 2020 at 13:30
  • 1
    @Gianna: Please post it as a separate question (and don't forget to accept this answer by clicking on the V next to it (if it answers your question of course)). Mar 18, 2020 at 13:37

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.