Search code examples
soliditysmartcontractshardhat

Error deploying smart contract using Hardhat -- Error HH9: Error while loading Hardhat's configuration


Trying to deploy a smart contract using Hardhat but getting configuration error.

Here is the complete error details

Error HH9: Error while loading Hardhat's configuration.
You probably tried to import the "hardhat" module from your config or a file imported from it.
This is not possible, as Hardhat can't be initialized while its config is being defined.

All the plug-ins seem to be have been installed correctly.

deploy.js

const hre = require("hardhat");

async function main() {
  const TestContract = await hre.ethers.getContractFactory("TestContract");
  const superInstance = await TestContract.deploy("TestContractHAT", "SMC");
  await superInstance.deployed();
  console.log("contract was deployed to:", superInstance.address());
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

package.json

{
  "name": "Test",
  "version": "1.0.0",
  "description": "This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, a sample script that deploys that contract, and an example of a task implementation, which simply lists the available accounts.",
  "main": "hardhat.config.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@nomiclabs/hardhat-ethers": "^2.0.5",
    "@nomiclabs/hardhat-waffle": "^2.0.3",
    "chai": "^4.3.6",
    "ethereum-waffle": "^3.4.4",
    "ethers": "^5.6.2"
  },
  "dependencies": {
    "dotenv": "^16.0.0",
    "hardhat": "^2.9.3"
  }
}

Hardhat.config

const { ethers } = require("hardhat");
require('@nomiclabs/hardhat-waffle');
require("@nomiclabs/hardhat-ethers");
require('@openzeppelin/hardhat-upgrades');


require("dotenv").config();
// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
  const accounts = await hre.ethers.getSigners();  
  for (const account of accounts) {
    console.log(account.address);
  }
});

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: "0.8.2",
  networks: {
    mumbai: {
      url: process.env.MUMBAI_URL,
      account: process.env.PRIVATE_KEY
    }
  }
};

Any pointers are helpful. Thanks


Solution

  • As the error states, you are importing hardhat module in your hardhat configuration.

    You probably tried to import the "hardhat" module from your config or a file imported from it. This is not possible, as Hardhat can't be initialized while its config is being defined.
    

    Remove the line

    const { ethers } = require("hardhat");
    

    and the error should disappear