Why is Hardhat throwing an error? I copy n pasted the default config file from https://hardhat.org/config/?

*Error HH8: There's one or more errors in your config file:
 Invalid value {"url":"https://eth-rinkeby.alchemyapi.io/v2/xxxxxxxxxx","accounts":[null]} for HardhatConfig.networks.rinkeby - Expected a value of type HttpNetworkConfig.*

To learn more about Hardhat's configuration, please go to https://hardhat.org/config/

 * @type import('hardhat/config').HardhatUserConfig


const privateKey1 = process.env.PRIVATE_KEY;
const etherscanKey = process.env.ETHERSCAN_API_KEY;

module.exports = {
  defaultNetwork: "rinkeby",
  networks: {
    hardhat: {},
    rinkeby: {
      url: "https://eth-rinkeby.alchemyapi.io/v2/oL89FHmvkiWEfUgdjWON4NZBnB-497Is",
      accounts: [privateKey1],
  solidity: {
    version: "0.8.0",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200,
  paths: {
    sources: "./contracts",
    tests: "./test",
    cache: "./cache",
    artifacts: "./artifacts",
  mocha: {
    timeout: 20000,

  • The error is telling you that privateKey1 is null or undefined. Maybe you are not setting the environment variables correctly? Oct 18, 2021 at 11:02

6 Answers 6


had to run source .env to enable the .env in Ubuntu 20.04 LTS

  • I got bash: .env: No such file or directory Dec 30, 2021 at 4:48

You're getting this error because hardhat needs to be able to verify that the URL and accounts you're using for a network are valid for that network. In your specific case, your Private key isn't working.

Two ways to fix:

1. Remove the networks you don't want

In your hardhat.config.js you can comment out or remove the network that is erroring. Your error is saying that:

Invalid value {"url":"https://eth-rinkeby.alchemyapi.io/v2/xxxxxxxxxx","accounts":[null]} for HardhatConfig.networks.rinkeby - Expected a value of type HttpNetworkConfig.*

So it's looking at the rinkeby URL and not finding a "good" one. Change your network config to:

        // rinkeby: {
        //     url: "https://eth-rinkeby.alchemyapi.io/v2/oL89FHmvkiWEfUgdjWON4NZBnB-497Is",
        //     accounts: [privateKey1],
        //   },

2. Fix your variables

You'll need to make sure your environment variables are actually getting pulled in correctly. Doing a console.log(process.env.PRIVATE_KEY) and console.log(process.env.RINKEBY_RPC_URL) are ways to check. Or if you're in linux, running echo $PRIVATE_KEY.

Once all your variables are working correctly, the config will work


I ran into this issue because I didn't import dotenv config. You can do so by adding this line in hardhat.config.js:


Note: OP did add this line, but this solution is for someone who forgot to import it.


Here are the possible reasons that you ran into this error: haven't import the dotenv, add this line to your config file


or its because you wrote account instead of accounts.

My error was I was using private key of another account while the RPC url I provided was from another account, I fixed it by using same private key.


Here the problem occurs in your private key problems not due to require('dotenv').config(); not adding. You already add this. In the .env file, you may not add your wallet's private key properly. After you may compile and deploy.


you need to place the .env file in the main folder, not a sub-folder, this will enable the program read the API_URL and PRIVATE_KEY files properly.

Not the answer you're looking for? Browse other questions tagged or ask your own question.