Tying to follow a basic NFT tutorial, and I have to say, that I am kind of a noob in Programming. The problem that I am facing is that my hardhat.config.js file needs an API_KEY and a Private Key, which it should import from the process.env file:
API_URL = ";
PRIVATE_KEY = "8d33c2613cb63d0dc6305e57..."
the hardhad config file looks like this:
* @type import('hardhat/config').HardhatUserConfig
*/
require('dotenv').config();
require("@nomiclabs/hardhat-ethers");
const { API_URL, PRIVATE_KEY } = process.env;
module.exports = {
solidity: "0.8.0",
defaultNetwork: "ropsten",
networks: {
hardhat: {},
ropsten: {
url: API_URL,
accounts: [`0x${PRIVATE_KEY}`]
}
},
}
But whenever I try to pile it and run it tru my deploy.js file I get an error message that essentially tells me, that the import was not possible, and looks like this:
* Invalid value undefined for HardhatConfigworks.ropsten.url - Expected a value of type string.
* Invalid value {"accounts":["0xundefined"]} for HardhatConfigworks.ropsten - Expected a value of type HttpNetworkConfig.
To learn more about Hardhat's configuration, please go to /
For more info go to or run Hardhat with --show-stack-traces
simon@MacBook-Pro-von-Simon test_fractals % npx hardhat run scripts/deploy.js --network ropsten
An unexpected error occurred:
simon@MacBook-Pro-von-Simon test_fractals % npx hardhat run scripts/deploy.js --network ropsten
An unexpected error occurred:
simon@MacBook-Pro-von-Simon test_fractals % npx hardhat run scripts/deploy.js --network ropsten
An unexpected error occurred:
ReferenceError: API_KEY is not defined
at Object.<anonymous> (/Users/simon/test_fractals/hardhat.config.js:37:12)
at Module._pile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at importCsjOrEsModule (/Users/simon/test_fractals/node_modules/hardhat/src/internal/core/config/config-loading.ts:23:20)
at Object.loadConfigAndTasks (/Users/simon/test_fractals/node_modules/hardhat/src/internal/core/config/config-loading.ts:66:18)
at main (/Users/simon/test_fractals/node_modules/hardhat/src/internal/cli/cli.ts:129:20)
simon@MacBook-Pro-von-Simon test_fractals % npx hardhat run scripts/deploy.js --network ropsten
An unexpected error occurred:
ReferenceError: API_KEY is not defined
at Object.<anonymous> (/Users/simon/test_fractals/hardhat.config.js:37:12)
at Module._pile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at importCsjOrEsModule (/Users/simon/test_fractals/node_modules/hardhat/src/internal/core/config/config-loading.ts:23:20)
at Object.loadConfigAndTasks (/Users/simon/test_fractals/node_modules/hardhat/src/internal/core/config/config-loading.ts:66:1
I first thought, that my .env file was not at the right place or that the syntax was not right, but after trying everything I could think of, I still get the same error message. Any help is appreciated.
Tying to follow a basic NFT tutorial, and I have to say, that I am kind of a noob in Programming. The problem that I am facing is that my hardhat.config.js file needs an API_KEY and a Private Key, which it should import from the process.env file:
API_URL = "https://ethropsten.alchemyapi.io/v2/UkW3oySI7WxvFwDwopQHPOHajHaWFZFv"
PRIVATE_KEY = "8d33c2613cb63d0dc6305e57..."
the hardhad config file looks like this:
* @type import('hardhat/config').HardhatUserConfig
*/
require('dotenv').config();
require("@nomiclabs/hardhat-ethers");
const { API_URL, PRIVATE_KEY } = process.env;
module.exports = {
solidity: "0.8.0",
defaultNetwork: "ropsten",
networks: {
hardhat: {},
ropsten: {
url: API_URL,
accounts: [`0x${PRIVATE_KEY}`]
}
},
}
But whenever I try to pile it and run it tru my deploy.js file I get an error message that essentially tells me, that the import was not possible, and looks like this:
* Invalid value undefined for HardhatConfigworks.ropsten.url - Expected a value of type string.
* Invalid value {"accounts":["0xundefined"]} for HardhatConfigworks.ropsten - Expected a value of type HttpNetworkConfig.
To learn more about Hardhat's configuration, please go to https://hardhat/config/
For more info go to https://hardhat/HH8 or run Hardhat with --show-stack-traces
simon@MacBook-Pro-von-Simon test_fractals % npx hardhat run scripts/deploy.js --network ropsten
An unexpected error occurred:
simon@MacBook-Pro-von-Simon test_fractals % npx hardhat run scripts/deploy.js --network ropsten
An unexpected error occurred:
simon@MacBook-Pro-von-Simon test_fractals % npx hardhat run scripts/deploy.js --network ropsten
An unexpected error occurred:
ReferenceError: API_KEY is not defined
at Object.<anonymous> (/Users/simon/test_fractals/hardhat.config.js:37:12)
at Module._pile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at importCsjOrEsModule (/Users/simon/test_fractals/node_modules/hardhat/src/internal/core/config/config-loading.ts:23:20)
at Object.loadConfigAndTasks (/Users/simon/test_fractals/node_modules/hardhat/src/internal/core/config/config-loading.ts:66:18)
at main (/Users/simon/test_fractals/node_modules/hardhat/src/internal/cli/cli.ts:129:20)
simon@MacBook-Pro-von-Simon test_fractals % npx hardhat run scripts/deploy.js --network ropsten
An unexpected error occurred:
ReferenceError: API_KEY is not defined
at Object.<anonymous> (/Users/simon/test_fractals/hardhat.config.js:37:12)
at Module._pile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at importCsjOrEsModule (/Users/simon/test_fractals/node_modules/hardhat/src/internal/core/config/config-loading.ts:23:20)
at Object.loadConfigAndTasks (/Users/simon/test_fractals/node_modules/hardhat/src/internal/core/config/config-loading.ts:66:1
I first thought, that my .env file was not at the right place or that the syntax was not right, but after trying everything I could think of, I still get the same error message. Any help is appreciated.
Share Improve this question edited Oct 11, 2021 at 11:47 Pritom Sarkar 2,2524 gold badges13 silver badges28 bronze badges asked Oct 11, 2021 at 11:39 Simon CosmosSimon Cosmos 611 silver badge2 bronze badges6 Answers
Reset to default 14I solved the same problem by using this in the hardhat.config.js
file:
require('dotenv').config({path:__dirname+'/.env'})
Instead of:
require('dotenv').config()
For more information, you can visit dotenv file is not loading environment variables
I ran in the same issue and I've solved it in a next way. The things back to normal when I firstly require the dotenv and only then set config params.
So, the final code looks like that:
const dotenv = require("dotenv");
dotenv.config({path: __dirname + '/.env'});
const { API_URL, PRIVATE_KEY } = process.env;
hope this save some time for someone =)
This worked for me
/**
* @type import('hardhat/config').HardhatUserConfig
*/
require('dotenv').config({path:__dirname+'/.env'})
require("@nomiclabs/hardhat-ethers")
const { API_URL, PRIVATE_KEY } = process.env
module.exports = {
solidity: "0.7.3",
defaultNetwork: "ropsten",
networks: {
hardhat: {
},
ropsten: {
url: "https://eth-ropsten.alchemyapi.io/v2/your-api-key",
// Replace your-api-key with your API key
// Your API_URL also stored in .env file but for simplity I have directly a string here
accounts: ["klaouoq84qoir983n2nc3234xn98349nx4u2394u23998x3n3"], // This is YOUR_PRIVATE_KEY from Metamask which you can store in env file but for simplity I have directly a string here
},
},
}
I had this issue because I added 'export' in front of my environment variables. Once I removed "export" I was able to access them from hardhat config
> npm i dotenv
require('dotenv').config()
...
module.exports = {
solidity: "0.8.17",
networks: {
hardhat: {
chainId: 80001
},
mumbai: {
url: `https://polygon-mumbai.g.alchemy./v2/${process.env.ALCHEMY_API_KEY}`,
accounts: [process.env.PRIVATE_KEY_WALLET]
}
}
};
To solve these problem I install dotenv extencion
npm install dotenv --save
And in hardtat.config I use process.env.YOUR_CONST
/**
* @type import('hardhat/config').HardhatUserConfig
*/
require('dotenv').config();
require("@nomiclabs/hardhat-ethers");
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.17",
defaultNetwork: "goerli",
networks: {
hardhat: {},
goerli: {
url:process.env.API_URL,
accounts: [`0x${process.env.PRIVATE_KEY}`]
}
},
};