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
    Commented Feb 7, 2021 at 19:47
  • @Ismael Think so. let web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:7545'));
    – Ayush
    Commented Feb 7, 2021 at 19:56
  • Are you sure ganache is running?
    – Ismael
    Commented Feb 7, 2021 at 20:06
  • @Ismael Yes. I can see the accounts with 100 ethers each. Any other way to make sure it is running?
    – Ayush
    Commented Feb 7, 2021 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
    Commented Feb 8, 2021 at 4:26

1 Answer 1

3

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

0

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.