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



试图使用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.

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

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

任何指针都是有用的。感谢

如错误所示,您正在安全帽配置中导入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");

并且错误应该消失

只需移除const{ethers}=require("hardhat"(;

或const{hardhatArguments}=require("hardhat"(;

或任何其他需要安全帽的