Metamask Configuration Error: Adding Hardhat as a Network
When setting up a local blockchain network for use with Metamask in hardhat, there are a few common errors that can occur. One of these issues involves incorrect RPC URLs. In this article, we will walk through what causes the error and provide step-by-step solutions to resolve it.
Error Message:
The error message usually looks like this:
New RPC URL:
Chain Id: 31337
Unable to retrieve chain ID. Is your RPC URL correct?
Understanding the Error
The error is caused by a misunderstanding of how Metamask resolves RPC URLs. indicates that the RPC server is running on the local machine, which is fine for testing purposes. However, when using hardhat to deploy and interact with the network, you need to specify the correct RPC URL.
The problem is likely caused by one of two reasons:
- Invalid Chain ID: The error message suggests that there is a problem retrieving the chain ID from Metamask. This is unlikely as it is a standard parameter required for RPC connections.
- RPC Server Not Running: In some cases, the RPC server may not be running or cannot be accessed at the specified URL. This can be due to various reasons such as network issues, permission errors, or configuration errors.
Step-by-step solutions:
To resolve this issue, follow these steps:
1. Verify that the RPC server is running
Verify that your Metamask instance is configured to run the RPC server at You can verify this by checking the Metamask settings under
Network >
RPC URL.
If you are using a custom RPC URL, make sure it is correct and matches the one specified in the error message.
2. Check for network issues
Try connecting to the local blockchain network using You can do this by copying the RPC URL from your Metamask instance:
wss://127.0.0.1:8545/
If you are still having problems, try restarting the RPC server or check for network connectivity issues.
3. Check the chain ID
Double-check that the chain ID is correct in your hardhat configuration file (usuallyhardhat.config.jsor
package.json). Make sure it matches the value specified in the error message (
31337`).
If you are still unsure about the chain ID, consider using a different RPC URL or double-checking your Metamask settings.
4. Update your hardhat configuration
If none of the above steps resolve the issue, try updating your hardhat configuration file to use the correct RPC URL and chain ID:
const { hardhatConfig } = require('./hardhat.config');
module.exports = hardhatConfig;
By following these steps, you should be able to resolve the “Adding hardhat as a network” error in your Metamask configuration.