-1

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

Nazlul
  • 1
  • 2
  • 1
    Does this answer your question? [Hardhat compile error with API\_URL and Private Key import](https://stackoverflow.com/questions/69525606/hardhat-compile-error-with-api-url-and-private-key-import) – Geshode Jun 06 '23 at 06:46

2 Answers2

0

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)

thomaux
  • 19,133
  • 10
  • 76
  • 103
0

Probably you have to set the path of the env file in the config parameter

require('dotenv').config({path: "the/path/used.env"); 

As is suggested on npm if your file containing environment variables is named or located differently.