DEV Community 👩‍💻👨‍💻

Luis Lucena
Luis Lucena

Posted on • Updated on

Hardhat bug fix with MetaMask due to chainId


  • This is a Hardhat error that originates because MetaMask takes the default network and hardhat works with another network. Here is a sample of the error and how to fix it.

Image description

Config your hardhat.config.js file

  • Manually add the chaindId:1337 to avoid the conflict with MetaMask


// The next line is part of the sample project, you don't need it in your
// project. It imports a Hardhat task definition, that can be used for
// testing the frontend.
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.9",
  networks: {
    hardhat: {
      chainId: 1337 // We set 1337 to make interacting with MetaMask simpler
Enter fullscreen mode Exit fullscreen mode

Boot up local development blockchain

$ npx hardhat node
Enter fullscreen mode Exit fullscreen mode

Connect development blockchain accounts to MetaMask

  • Copy private key of the addresses and import to MetaMask
    Connect your MetaMask to hardhat blockchain, network

  • If you have not added hardhat to the list of networks on your MetaMask, open up a browser, click the fox icon, then click the top center drop down button that lists all the available networks then click add networks. A form should pop up. For the "Network Name" field enter "Hardhat". For the "New RPC URL" field enter "". For the chain ID enter "1337". Then click save.

Migrate Smart Contracts

$ npx hardhat run src/contracts/scripts/deploy.js --network localhost
Enter fullscreen mode Exit fullscreen mode


  • Once you have your test account on the hardhat node you will have 10000 ETH to deploy your contracts.

Happy coding!❤️
💫 contributions are appreciate..
ETH address: luislucena.eth
tw: _luisald

Oldest comments (0)

Classic DEV Post:


Web Caching Explained by Buying Milk at the Supermarket