0

I'm just trying to get web3.eth in node.js to tell me what the current block number is but I get an error 'UnhandledPromiseRejectionWarning: Error: Invalid JSON RPC response: ""', I have "geth --syncmode "light"" running in a different console tab and I thought that would be all I needed to get a data feed from the main-net, is there something I'm just not understanding or is it something about my environment that is causing this

var Web3 = require('web3');
var web3 = new Web3("http://localhost:8545");
web3.eth.getBlockNumber().then(console.log);

node v14.4.0

npm v6.14.5

"dependencies": {

"ethereumjs-testrpc": "^6.0.3",
"web3": "^1.2.9",
"web3-eth": "^1.2.9"

}

3
  • Have you set up an Ethereum node (Geth, Parity, Ganache, etc) running on your local machine and listening on port 8545? Commented Jun 24, 2020 at 20:50
  • yes I have a geth "light" client running and by default, it listens to port 8545 Commented Jun 25, 2020 at 4:54
  • @RileyDesrochers See this answer ethereum.stackexchange.com/questions/30245/…. RPC api is disable by default, you have to explicitly enable if you intend to use it.
    – Ismael
    Commented Jun 25, 2020 at 19:33

1 Answer 1

0

the error is caused because you don't have enabled the HTTP-RPC server.

To enable it you can add the --rpc option to the geth --syncmode "light" command.

Also you can decide which API enable by listing them in the --rpcapi option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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