8

I have started a Hardhat network with default configuration using npx hardhat node. After running this command, I see Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/ and a list of 20 seed accounts along with their private keys.

Then, in Metamask, I've gone to "Custom RPC" network configuration, and entered the following:

Network Name: Hardhat

New RPC URL: http://localhost:8545

Chain ID: 31337

But when I try to apply the changes, I see this error: Could not fetch chain ID. Is your RPC URL correct?

If I enter http://localhost:8545 in my web browser, I see this JSON: {"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error: Unexpected end of JSON input"}}

I am able to successfully run code that connects to the local Hardhat network and passes tests. I'm able to successfully run Hardhat tasks by using commands like npx hardhat TASKNAME --network localhost. I've tried using 127.0.0.1 instead of localhost in the Metamask config. I'm using WSL2 to run Bash and the Hardhat server.

1

4 Answers 4

6

The issue turned out to be caused by the browser I was using: Brave. It has some default ad blockers called "shields". These somehow prevented me from connecting the Metamask extension to my local network. In Brave settings for shields, I set "Trackers & ads blocking" to "disabled", and the issue was resolved.

4

I had the same issue in chrome. I've added a new network with the same RPC URL: http://localhost:8545 with chainId 31337 and i was able to connect to the local network.

1

yarn hardhat node (run this command into project terminal and then try to run )

-1

Add this to your hardhat.config.js and then do npx hardhat node. After that it should not show any error in your metamask.

networks: { hardhat: { chainId: 1337 }

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.