I am running a local ganache-cli as ganache-cli -p 8545.

Furthermore, I created a frontend script using web3:

<html lang="en">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My Dapp</title>



    <h1>This is my balance</h1>
    <button onclick="getBalance()">Update my balance </button>
    <span id="myBalance"></span> in Ether.

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="js/web3.min.js"></script>
    <script type="text/javascript">
        if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
        } else {
            // set the provider you want from Web3.providers
            web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));


        function getBalance() {
            document.getElementById("myBalance").innerText = web3.fromWei(web3.eth.getBalance(web3.eth.accounts[0]),


The script basically updates my balance which I would have in ether.

However, my web3.eth.accounts[0] is undefined. See my console.log below:

enter image description here

Any suggestion what might be wrong?

I appreciate your replies!


I added the following to my code:

    console.log("Log: (web3.eth)");
    console.log("Log: (web3.eth.accounts)");

enter image description here enter image description here

  • 1
    Do you mean that web3.eth.accounts is []? – user19510 Feb 8 '18 at 17:46
  • Which branch of the if is being taken here? Are you using an HttpProvider or some provider injected into the page (e.g. MetaMask)? – user19510 Feb 8 '18 at 17:47
  • @smarx web.eth.accounts is undefined when executing my code. I am running a ganache locally. Please see my update. – Soli Feb 8 '18 at 18:55
  • 1
    Your comment says web.eth.accounts is undefined (a typo for web3.eth.accounts?), but your post says that web3.eth.accounts[0] is undefined, which means web3.eth.accounts must be defined. I know you're using ganache-cli, but I still need to know which branch of the if is being taken. If you're not sure, please add logging in each branch so we can find out. – user19510 Feb 8 '18 at 18:59
  • @smarx I added an update to my question. Basically web3.eth has an accounts branch. Any suggestions how to access this? – Soli Feb 8 '18 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.