代码之家  ›  专栏  ›  技术社区  ›  BlockChain Learner

使用Hardhat部署智能合约时出错--错误HH9:加载Hardhat配置时出错

  •  0
  • BlockChain Learner  · 技术社区  · 3 年前

    试图使用Hardhat部署智能合约,但出现配置错误。

    以下是完整的错误详细信息

    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.
    

    所有的插件似乎都安装正确。

    部署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);
      });
    
    

    包裹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"
      }
    }
    

    安全帽。配置

    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
        }
      }
    };
    
    

    任何提示都是有用的。谢谢

    0 回复  |  直到 3 年前
        1
  •  1
  •   keser    3 年前

    如错误所述,您正在导入 hardhat 安全帽配置中的模块。

    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.
    

    拆下电话线

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

    错误应该消失