25

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?

4
  • 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 Aug 5, 2017 at 5:49
  • 1
    As per your link, you are running the testrpc. Try to check if testrpc is running or not.
    – Abhishek
    Aug 5, 2017 at 5:56
  • 1
    Thanks Abhishek! I didn't realize I needed testrpc to be running in another terminal window. That solved it for me.
    – tim_xyz
    Aug 5, 2017 at 17:09
  • 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 :-) Aug 25, 2017 at 18:56

6 Answers 6

20

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 :

mahesh@projectblockchain:~/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.

7
  • I get error after following all the commands you mentioned Error: Invalid JSON RPC response: undefined at Object.InvalidResponse
    – Ashit Vora
    Sep 12, 2017 at 5:31
  • I did that but dont get any response. No failures either, it just freezes
    – Diego
    Nov 7, 2017 at 17:51
  • In windows to start RPC from your application root it should be node_modules\.bin\testrpc Dec 11, 2017 at 17:25
  • 2
    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? Apr 11, 2018 at 20:22
  • 1
    @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 Apr 14, 2018 at 5:06
3

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

2

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"));
1
  • 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. Mar 11, 2020 at 14:31
1

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();
1

I had the same issue. Ganache was running on http://127.0.0.1:7545

I replaced this line

const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"))

with this one:

const web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:7545"))

And it worked

0
0

Go to settings in Ganache Desktop app and change hostname in server settings. Settings -> server -> hostname enter image description here

set [All Interfaces] then try, running your js file with node following.

const Web3 = require("web3");

var web3 = new Web3(new Web3.providers.HttpProvider("http://0.0.0.0:7545"));
try {
  console.log(web3.eth.accounts);
} catch (e) {
  console.log(e.message);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.