Search code examples
javascriptweb3js

`Web3.utils` is undefined and `Web3.eth` is undefined


After upgreading Web3 from 1.0.0-beta.34 to 1.0.0-beta.37 a try to access any properties of Web3.utils, Web3.eth, Web3.bzz and Web3.shh will give an error. For example, calling web3.utils.utf8ToHex('some string') at 2_deploy_contract.js as follow:

const Web3 = require('web3');
web3.utils.utf8ToHex('some string');

Is raising the following exception:

> TypeError: Cannot read property 'utf8ToHex' of undefined

And I found that the following code snippet:

const Web3 = require('web3');
console.log("Web3.eth = " + Web3.eth)
console.log("Web3.utils = " + Web3.utils)
console.log("Web3.bzz = " + Web3.bzz)
console.log("Web3.shh = " + Web3.shh)

Will output:

> Web3.eth = undefined 
> Web3.utils = undefined 
> Web3.bzz = undefined
> Web3.shh = undefined

Solution

  • Static properties (Web3.utils, Web3.eth and etc...) got removed because if someone is just using the utils then he should probably use the web3-utils and web3-eth module directly, instead of bundling the complete library (ref).

    Sample working code:

    const Web3Utils = require('web3-utils');
    const someString = Web3Utils.utf8ToHex('some string');
    

    Note: The long term goal will be to remove the Web3 class and to create the Web3 Namespace. But this will be done after the stable release (ref).