I included dist/web3.js (1.0.0-beta36) in my html file as below

<script type="text/javascript" src="./web3.js"></script>
<script type="text/javascript">
    var Web3 = require('web3');        
    var web3 = new Web3();

    window.onload = function() {
        web3.setProvider(new web3.providers.HttpProvider("https://api.myetherwallet.com/eth"));
        var account = web3.version;
        var balance = web3.eth.blockNumber;
        balance = 0;

When I launch the html with Firefox/Chrome it throws exception Error: Cannot find module 'buffer' then follow by Uncaught ReferenceError: require is not defined

If I downgrade the version of web3.js to v0.20.6, it functions ok. Did I missed something?

1 Answer 1


There are two main issues here:

  1. require is a part of the Node.js module system, and will not work on standard HTML page, nor do you need it.

  2. The function you want to call to get the current block number is web3.eth.getBlockNumber(), which is an asynchronous function, so you will also need to handle that.

Your code will work if written like so:

<script type="text/javascript" src="./web3.js"></script>
<script type="text/javascript">
window.onload = async function () {
    var web3 = new Web3('https://api.myetherwallet.com/eth');
    var version = web3.version;
    var blockNumber = await web3.eth.getBlockNumber();

    console.log(version, blockNumber);


Here is a working example: https://jsfiddle.net/d8rus2hc/

  • The solution works ok with chrome browser, but with Firefox for some reason it never comes out from 'await'
    – Consy
    Oct 25, 2018 at 7:25
  • I use Firefox, and it works fine for me. It could be a problem with the Web3 provider from myetherwallet, since you are querying a node and then waiting for a response. How are you testing? with the JSFiddle? Oct 25, 2018 at 7:31
  • Using your example works with Firefox. If I replace the 'web3.js' pointing to my local file it will fail. The failure is caused by browser security "Cross-Origin Request Blocked". I think this is off topic, i'll accept your answer.
    – Consy
    Oct 25, 2018 at 7:38

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.