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!!
Best Answer
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)
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