Geth RPC running but cannot connect in Web3 nodejs

by Pages   Last Updated January 14, 2018 06:28 AM

Wracking my brains here, geth is running the RPC as expected but I can't connect to it with web3 I'm running like so:

geth --rpc --rpccorsdomain "*" --rpcapi "eth,web3"

I'm running this in screen and see in the terminal it starts running the RPC API and then continues to sync the blockchain.

I have confirmed the API is running:

curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' http://localhost:8545

Which returns as expected:


I'm running nodejs v6.12.3 and npm install web3 this morning. The nodejs script I'm trying is:


var Web3 = require('web3');
var web3 = new Web3(); // returns as expected

web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));

var coinbase = web3.eth.coinbase;
console.log(coinbase); // undefined

I noticed that after running setProvider inspecting the web3 object I see:

_provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },

I've tried a number of other web3.eth API's but no luck. How can I get it connected? Have I fundamentally misunderstood something here?

Answers 1

npm installs the new version of the API by default now. You can check with:

var Web3 = require('web3');
var web3 = new Web3();

If it's >1.0.0 you need to refer to this documentation:

Or get the old version:

npm uninstall web3
npm install web3@0.20.3

I should not be allowed around computers.

January 14, 2018 05:55 AM

Related Questions

How to check currently installed web3 version?

Updated August 17, 2018 06:28 AM

Send payment from wallet using web3

Updated March 24, 2018 13:28 PM

Insall web3 on Ubuntu 16.04

Updated April 21, 2018 07:28 AM