7

I always use following command to create a contract instance without any problem :

var contractInstance = new web3.eth.Contract(abi, '0xe1623AAf57fCbe260F022404C730ae32aebe39F6');

However, when currently I use this command, I receive following error :

TypeError: web3.eth.Contract is not a constructor

What is the reason ?

1
  • I'm pretty sure it's contract (starting with lower-case c). At least that's the case in web3.py, and would assume that it is also the case in web3.js. May 7, 2018 at 15:48

2 Answers 2

11
TypeError: web3.eth.Contract is not a constructor

This is a generic javascript error: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_constructor

You call a 'new' on an object ('web3.eth.Contract') without a constructor.

It looks like your jumping from web3 versions.

In web3 < 1.0, this is the syntax (lowercase c, no 'new'):

var MyContract = web3.eth.contract(abiArray, contractAddress);
var version = web3.version.api; // "0.2.0"

In web3 1.0 (not ready for production yet) this is the syntax (uppercase C, 'new'):

var MyContract = new web3.eth.Contract(abiArray, contractAddress);
var version = web3.version; // "1.0.0"
1
  • 1
    Note that the contract keyword in web3<1.0 has the lower case c letter. Nov 22, 2018 at 22:05
2

I think you are using the web3 < 1.0 which support this syntax:

var MyContract = web3.eth.contract(abiArray, contractAddress);

Your code example with the new is for web3@^1.0

1
  • In fact, as user goodvibration commented, I must use the lower case of "contract" like this : "web3.eth.contract".
    – Questioner
    May 7, 2018 at 16:05

Your Answer

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

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