0

Below is the simple programme which I wrote to use EVM compatible functions in VS code.

let web3 = require('web3');
const tr = new web3(web3.givenProvider || "ws://localhost:8545");   // Line 1
discuss();
async function discuss()
{
   const latest = await tr.eth.getBlock(1);
    console.log(latest);
   
    
}

I used the value present in Line 1 from reading documentationhere .

However , I am getting the below error :-

enter image description here

I have installed web3 package using npm i web3. Is there anything else I need to do . In the documentation it was written that "Port 8545 is the default listening port for the Remote Procedure Call (RPC) interface of Ethereum clients" . Is there any more configuration which I am missing or like what is wrong here ?

2 Answers 2

2

You don't have a node running in localhost.

You can use ganache (docs) to set up a local node like this:

npm install ganache --global
ganache

It should be automatically hosted at port 8545.

Another alternative tool is hardhat (docs):

npm install hardhat
npx hardhat node

Remember to do this in a separate terminal and keep it running until you need it.

6
  • So do I need to run these 2 commands i.e. npm install ganache --global and 'ganache' everytime i need to test.
    – Brijesh
    Commented Jan 28, 2023 at 6:25
  • I ran the command in one terminal of vs code and ran the pgm in another it did not work, however when i ran the ganache command in command prompt and then ran the pgm in Vs code , it is working. Is it the right behaviour ?
    – Brijesh
    Commented Jan 28, 2023 at 6:37
  • @Brijesh npm install ganache --global it's for installing, run only once. ganache it's every time you need a node. If it's working you did the right thing :)
    – 0xSanson
    Commented Jan 28, 2023 at 10:41
  • but i think , I need to run ganache command everytime i need to run the pgm , correct ?
    – Brijesh
    Commented Jan 28, 2023 at 12:07
  • yes, you need it to create a blockchain locally.
    – 0xSanson
    Commented Jan 28, 2023 at 17:41
0

The error message you're seeing indicates that there's no Ethereum client running on port 8545 on your local machine. This is the port that the code is trying to connect to in order to communicate with the Ethereum network.

Here are a few things to check:

Make sure that you have an Ethereum client (such as Geth or Parity) running on your local machine and that it's configured to listen on port 8545 for RPC requests.

If you're using Geth, you can start it with the following command:

css

geth --rpc --rpcport 8545

If you're using Parity, you can start it with the following command: css

parity --jsonrpc-port 8545 You can also check if the client is running on correct port by visiting javascript

http://localhost:8545/

If you're using an Ethereum testnet, make sure that you have the correct endpoint URL for the testnet you're using. For example, if you're using Rinkeby, the endpoint URL would be http://rinkeby.infura.io/v3/YOUR_PROJECT_ID

If you're trying to connect to a remote node, make sure that the node is accessible and has the correct endpoint URL.

You can also check if you have the correct web3 version installed.

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.