skip to Main Content

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


  1. 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)

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search