5

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
 */
require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-ethers");
require("@nomiclabs/hardhat-truffle5");
require("@nomiclabs/hardhat-etherscan");
require("hardhat-deploy");

require("dotenv").config();

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,
  },
};

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

6 Answers 6

4

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

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

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

1

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

require('dotenv').config();

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

0

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

require('dotenv').config();

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.

0

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.

0

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.