Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'toWei' of undefined #2328

Closed
adnantium opened this issue Feb 5, 2019 · 4 comments
Closed

TypeError: Cannot read property 'toWei' of undefined #2328

adnantium opened this issue Feb 5, 2019 · 4 comments

Comments

@adnantium
Copy link

There are similar looking older (2018) versions this problem but it was working fine until recently. Seems to have made a comeback after v1.0.0-beta.37

Expected behavior

$ node
> const web3 = require('web3');
> web3.utils.toWei('0.0001')
'100000000000000'

Actual behavior

$ node
> const web3 = require('web3');
> web3.utils.toWei('0.0001')
Thrown: TypeError: Cannot read property 'toWei' of undefined

Steps to reproduce the behavior

See 'Actual behavior'

Error Logs

Thrown: TypeError: Cannot read property 'toWei' of undefined

Versions

  • web3.js -> web3@1.0.0-beta.41
  • uname -a -> Darwin nsa-mbp2.local 17.7.0 Darwin Kernel Version 17.7.0: Fri Nov 2 20:43:16 PDT 2018; root:xnu-4570.71.17~1/RELEASE_X86_64 x86_64
  • node --version -> v11.9.0
  • npm --version -> 6.5.0
@zachlysobey
Copy link

yea, I've duplicated this on web3@1.0.0-beta.38 :-/

@adnantium
Copy link
Author

Another detail: It works if you access toWei() thru an instance that has been created with a provider url rather than directly thru the library.

$ node
> const Web3 = require('web3');
> const web3 = new Web3('http://localhost:7545');
> web3.utils.toWei('0.0001')
'100000000000000'

Hack workaround: use web3-utils directly to access toWei() if you need something from web3.utils when you dont have an initialized and connected web3 instance yet.

$ node
> const web3_utils = require('web3-utils');
> web3_utils.toWei('0.0001')
'100000000000000'

It seems that toWei() is not accessible thru web3.utils anymore (changed around v beta.41). Not sure if that a change by design or a bug. Understandably: beta software evolves fast and will break backwards compatibility occasionally.

@nivida
Copy link
Contributor

nivida commented Feb 6, 2019

The static property utils do no longer exist on the Web3 class. If you would like to use the web3-utils module then please install just the web3-utils module.

import * as Utils from 'web3-utils';

// or
import { asciiToHex } from 'web3-utils';

EDIT:

I've planned to export all the modules directly from the "web3" NPM package but until then you have to install such a module separately.

@nivida nivida closed this as completed Feb 6, 2019
@zachlysobey
Copy link

@nivida the following is still in the web3.js 1.0 documentation:

utils

Static property of the Web3 class.
Web3.utils
Utility functions are also exposes on the Web3 class object directly.

probably worth being very careful about updating the docs when there is a breaking change, otherwise you're gonna get a lotta folks submitting issues like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants