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:

{"jsonrpc":"2.0","id":67,"result":"Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9"}

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

#!/usr/bin/nodejs

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();
web3.version

If it's >1.0.0 you need to refer to this documentation: http://web3js.readthedocs.io/en/1.0/index.html

Or get the old version:

npm uninstall web3
npm install web3@0.20.3

I should not be allowed around computers.

Pages
Pages
January 14, 2018 05:55 AM

Related Questions


Problem to install web3 module. Ubuntu

Updated August 30, 2017 14:28 PM

geth "too many open files" on ubuntu

Updated July 07, 2017 11:28 AM


How do I uninstall ethereum-unstable from ubantu 17.10?

Updated November 16, 2017 18:28 PM

How can I run go-ethereum as daemon process on Ubuntu?

Updated December 18, 2017 16:28 PM