Invalid JSON RPC response: undefined, when running web3.eth.accounts (Node)

  • I'm very new Ethereum and trying to follow this tutorial:

    https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-1-40d2d0d807c2

    But when I run:

    web3.eth.accounts
    

    I get an error:

    Error: Invalid JSON RPC response: undefined
        at Object.InvalidResponse (/Users/me/Ethereum/hello_world_voting/node_modules/web3/lib/web3/errors.js:38:16)
        at HttpProvider.send (/Users/me/Ethereum/hello_world_voting/node_modules/web3/lib/web3/httpprovider.js:95:18)
        at RequestManager.send (/Users/me/Ethereum/hello_world_voting/node_modules/web3/lib/web3/requestmanager.js:58:32)
        at Eth.get [as accounts] (/Users/me/Ethereum/hello_world_voting/node_modules/web3/lib/web3/property.js:107:62)
        at repl:1:9
        at ContextifyScript.Script.runInThisContext (vm.js:44:33)
        at REPLServer.defaultEval (repl.js:239:29)
        at bound (domain.js:301:14)
        at REPLServer.runBound [as eval] (domain.js:314:12)
        at REPLServer.onLine (repl.js:433:10)
    

    Everything up until this worked.

    Looks like an associated question here, but I don't have enough background to understand and follow it.

    Error: Invalid JSON RPC response: undefined from node web3.js app

    Can anyone point me in the right direction?

    do you execute command 'geth --datadir=./dev/data0 --networkid 1 --rpc 8545 console' on terminal? and then execute 'geth --networkid 1 --datadir=./dev/data0 --dev attach ipc:./dev/data0/geth.ipc' on other terminal

    As per your link, you are running the testrpc. Try to check if testrpc is running or not.

    Thanks Abhishek! I didn't realize I needed `testrpc` to be running in another terminal window. That solved it for me.

    Hi @tim_xyz - could you add an answer to your own question explaining in a bit more detail what you needed to do to solve this? It might help others in future, and it'll stop this question from going stale and unanswered :-)

  • I had the same issue, and just found the solution. This response happens when testrpc is not running, as stated by @Abhishek in the comments.

    To solve it, the trick is to run testrpc in another terminal window:

    Install web3 and testrpc packages: npm install ethereumjs-testrpc [email protected] In the same terminal window, run testrpc: node_modules/.bin/testrpc Open a NEW terminal window (with the first one still open and running), and run node :

    [email protected]:~/hello_world_voting$ node
    > Web3 = require('web3')
    > web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    

    Finally, you can run: > web3.eth.accounts

    You will get the list of accounts.

    I get error after following all the commands you mentioned `Error: Invalid JSON RPC response: undefined at Object.InvalidResponse`

    I did that but dont get any response. No failures either, it just freezes

    In windows to start RPC from your application root it should be node_modules\.bin\testrpc

    To clarify, testrpc is just a mocked test node right? If you're running a real node and want to debug an issue with the real node, you should not be using testrpc right?

    @marcvander TestRPC is for test network.. What if i want to transfer real coin on tokens on main network programmatically ? what is the command for running main network instead of TestRPC?? Please Help

    @obesechicken13 yes TestRPC is just a mocked test node

    @veereshkumbar TestRPC is not event for test network, it is just running on your computer. The second part of the tutorial written by Mahesh Murthy explains how to deploy your contract to the public test network, so everyone can see it: https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-2-30b3d335aa1f

  • I had the same issue, but i solved this problem as: before run web3.eth.accounts, you need to open new terminal, go to your folder project and run command testrpc, after that you can run web3.eth.accounts without error

  • I had this problem but for a different reason. I was trying to access Ganache (which is now also TestRPC) using an HTTPS URL instead of an HTTP URL:

    Wrong:

    https://127.0.0.1:8545
    

    Right:

    http://127.0.0.1:8545
    

    Assuming you have Ganache listening on port 8545.

    TIP: Grab the URL right from the Ganache GUI. You'll see it at the top of the screen. If you are using the Ganache CLI version, grab it from the startup messages you should see in the command window you launched it from. Then just paste it into your web3 object initialization line when you create that object in your Javascript code:

    let web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
    

    My case with **infura** url was vice versa. Infura forces `https`, my connection url was based on `http` and it cause an hour of headache.

  • To make access to web3 and use it to retrieve a list of accounts that can be automatically created for you as part of the local ganache network.

    You start out by defining a beforeEach() statement where you can add an arrow syntax and add some comments that can guide you in completing these tasks.

    So how do you gain access to web3 to gain access to all unlocked accounts?

    Inside your beforeEach() you access your web3 module which can be used with many different types of cryptocurrencies. In this case you are looking for ethereum currency so it will look like this: web3.eth.

    Then you will want to access the getAccounts() function. web3 is asynchronous in nature, which means it will always be returning a promise and you want it to return a promise with a list of accounts that you care about. So you have to implement async/await like so below:

    const assert = require('assert');
    const ganache = require('ganache-cli');
    const Web3 = require('web3');
    
    const provider = ganache.provider();
    const web3 = new Web3(provider);
    
    let accounts;
    
    beforeEach(async () => {
      // Get a list of accounts
      accounts = await web3.eth.getAccounts();
    

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used