hardhat.config.js
require("@nomicfoundation/hardhat-toolbox");
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.18",
};
/* @type import('hardhat/config').HardhatUserConfig*/
require("@nomiclabs/hardhat-ethers");
require('dotenv').config();
const { API_URL_KEY, PRIVATE_KEY } = process.env;
module.exports = {
solidity: "0.8.17",
defaultNetwork: "goerli",
networks: {
hardhat: {},
goerli: {
url: API_URL_KEY,
account: [`${PRIVATE_KEY}`]
}
},
}
.env
PRIVATE_KEY=b14......3f
API_URL_KEY=https://alien-few-river.ethereum-goerli.discover.quiknode.pro/..../
npx hardhat compile
An unexpected error occurred:
ReferenceError: API_URL_KEY is not defined
2
Answers
I believe you need to move the import of dotenv (
require('dotenv').config();
) all the way to the top of your file, or at least before the import statement of the hardhat toolbox (which in your case, is at the top of your file)Probably you have to set the path of the env file in the config parameter
As is suggested on npm if your file containing environment variables is named or located differently.