Go-Ethereum – Making Web3.js Work from Node Server Code

go-ethereumjavascriptnodejsweb3js

I'm running a geth node on the testnet and trying to run web3.js method from my node server code.

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

However, I'm getting a provider not set or valid error. Who exactly is the provider here. Briefly, how do I bind my web3.js to my running geth client?

Best Answer

Where did your web3 object come from? It needs to be constructed with a Provider that tells it how to connect to an Ethereum node, like this:

var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));