1

Trying to connect Ganache GUI running on port 7545 to web3.js. Getting the below error whenever trying to call any of the functions of web3 -

> web3.eth.getBalance("0x907A41F173099E5F5573F0ab43B6Dd9594e3687a").then(console.log);
Promise { <pending> }
> Uncaught Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/mnt/c/.../web3_development/node_modules/web3-core-helpers/lib/errors.js:43:16)
    at XMLHttpRequest.request.onreadystatechange (/mnt/c/.../web3_development/node_modules/web3-providers-http/lib/index.js:95:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/mnt/c/.../web3_development/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (/mnt/c/Users/.../web3_development/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpRequestError (/mnt/c/.../web3_development/node_modules/xhr2-cookies/dist/xml-http-request.js:349:14)

Running node on WSL-2.

Any help as to what might be causing the issue or ways to resolve it would be really helpful.

Thanks a lot!!

10
  • Are you connecting to the right port? – Ismael Feb 7 at 19:47
  • @Ismael Think so. let web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:7545')); – Ayush Feb 7 at 19:56
  • Are you sure ganache is running? – Ismael Feb 7 at 20:06
  • @Ismael Yes. I can see the accounts with 100 ethers each. Any other way to make sure it is running? – Ayush Feb 7 at 20:17
  • 1
    @Ismael Found that WSL2 cannot connect to localhost. Ganache has got the option to change host to WSL. It works now. Thanks a lot for all your help – Ayush Feb 8 at 4:26
2

Came to know that WSL-2 cannot connect to the localhost directly. See this and this.

WSL-2 has its own port to listen to. Ganache gives you the option to select that port instead of localhost.

Go to Settings > Server > Hostname > 172.23.160.1 - Ethernet (WSL) ganache settings

Can use the command provided by @Ismael above in the comments to check if the port is connected. It should return the block number. -

curl --data '{"method":"eth_blockNumber","params":[],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST 127.0.0.1:7545

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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