Skip to content
EthNews

News from across Ethereum

Hardhat Chain ID Mismatch: Config and RPC Checks

Fix a Hardhat chain ID mismatch on a custom network by comparing hardhat.config with the RPC’s eth_chainId, correcting the URL or decimal chainId, then redeploying.

The EthNews Desk4 min read

Hardhat Chain ID Mismatch: Config and RPC Checks

Hardhat’s chain ID mismatch error is fixed by making the configured network’s chainId match the RPC node’s eth_chainId, or by correcting the RPC URL if it reaches a different chain. Hardhat compares these values before sending a deployment transaction, so the deploy halts before anything is broadcast.

Why Hardhat stops before deployment

A mismatch means the chain ID in hardhat.config and the ID reported by the selected node differ. The error usually shows both values. Record them, along with the network name you passed to Hardhat, before changing the configuration.

The common causes are a mistyped ID, a hexadecimal value copied into a field expected to be decimal, or an RPC URL environment variable that resolves to another chain. These are distinct problems: changing chainId can fix a mistaken setting, but it cannot make the wrong endpoint point to the intended network.

Trace the configured network

Use the RPC URL configured for the named network to ask the node which chain it serves. Compare that response with the chain you intended to deploy to.

  1. Note both chain IDs in the error and the network name in your deployment command. In hardhat.config, locate that name under networks and identify its url and chainId values. If url comes from an environment variable, note the variable’s current value source as well.

  2. Send a JSON-RPC request to that exact URL using the eth_chainId method. With curl, the request body is {"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}; include a Content-Type: application/json header. The result is a hexadecimal quantity such as 0x1 or 0xaa36a7.

  3. Convert the returned hexadecimal value to decimal, then compare it with the chain ID shown in the error and the intended chain’s entry on Chainlist. Chainlist records network chain IDs alongside RPC endpoints, which helps distinguish a wrong config value from a URL aimed at the wrong network. Use the endpoint you queried as the reference for what that node actually reports.

  4. Correct the setting that is wrong. If the URL reports the intended chain’s ID, set that network’s chainId to the matching decimal number in hardhat.config. If it reports a different chain, fix the URL or its environment variable instead, then query the corrected endpoint. Chainlist can help confirm the intended network’s ID and available endpoint details.

  5. Rerun the deployment with the same configured network name, using npx hardhat run scripts/deploy.js --network followed by that name. Confirm the output includes a transaction hash, then verify the transaction on the intended chain. A hash printed by the deployment confirms Hardhat passed the chain ID check and submitted the transaction; verifying the destination confirms it ran against the network you selected.

Account for local forks

A local Hardhat fork has its own chain ID behavior: hardhat node --fork uses the built-in Hardhat network, which reports 31337 by default, even when the forked RPC serves a chain with a different ID. The fork copies chain state for local execution; it does not automatically adopt the upstream network’s chain ID.

If the mismatch is on that local fork, compare the ID returned by the local node with the configured value. Set networks.hardhat.chainId if you need the local network to report a different ID. This setting is separate from the chainId on a custom remote network entry, so changing the remote entry will not change the local fork’s reported ID.

EIP-155 uses chain IDs to distinguish transaction signing across chains. That is why the configured ID matters to deployment: a transaction prepared for one chain ID is not interchangeable with one prepared for another. The mismatch check catches disagreement before Hardhat sends the deployment transaction.

Finish with the intended network

A successful redeploy should print a transaction hash after the chain ID comparison passes. Confirm the transaction on the chain you intended to target; that final check ties the deployment to both the configured network name and the RPC endpoint you verified.

Once confirmed, keep the network name and its URL together in your deployment command and environment configuration. That gives the next deployment the same verified endpoint and lets you catch a changed URL before it sends a transaction.