11

I have an ALCHEMY_MAINNET_RPC_URL environment that is of type string. I can verify it as such with:

const ALCHEMY_MAINNET_RPC_URL = process.env.ALCHEMY_MAINNET_RPC_URL || ''
console.log(typeof (ALCHEMY_MAINNET_RPC_URL))

And I get the output string.

In my config, I have a network setup as such:

const config: HardhatUserConfig = {
    defaultNetwork: 'hardhat',
    networks: {
        mainnet_fork: {
            chainId: 1,
            forking: {
                url: ALCHEMY_MAINNET_RPC_URL,
            }
        },

When trying to add this to my hardhat.config.ts, I keep getting the following error:

Error HH8: There's one or more errors in your config file:
  • Invalid value undefined for HardhatConfig.networks.mainnet_fork.url - Expected a value of type string.

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

I've tried some different approaches like:

const ALCHEMY_MAINNET_RPC_URL = process.env.ALCHEMY_MAINNET_RPC_URL
const ALCHEMY_MAINNET_RPC_URL = process.env.ALCHEMY_MAINNET_RPC_URL!

but then I get:

Type 'undefined' is not assignable to type 'string'.

Or the same error.

Any thoughts?

Patrick Collins
  • 11,186
  • 5
  • 44
  • 97
  • 2
    In my case, I put the .env file in a sub dir by mistake, move it to project root dir, then it works. – Eric May 25 '22 at 17:15

9 Answers9

6

If you are using .env install dotenv using

npm install dotenv --save

or as in my case, I have forgotten to add

require("dotenv").config();

on top of the hardhat.config.js file

aakash verma
  • 61
  • 1
  • 2
2

It's not complaining about the URL inside the forking. You need a URL for the hardhat environment to run on:

const config: HardhatUserConfig = {
    defaultNetwork: 'hardhat',
    networks: {
        mainnet_fork: {
            url: 'http://127.0.0.1:8545'
            chainId: 1,
            forking: {
                url: ALCHEMY_MAINNET_RPC_URL,
            }
        },

...or you have a .env file in the wrong directory.

Patrick Collins
  • 11,186
  • 5
  • 44
  • 97
1

You have both ethereum-waffle and @nomicfoundation/hardhat-chai-matchers installed. They don't work correctly together, so please make sure you only use one.

We recommend you migrate to @nomicfoundation/hardhat-chai-matchers. Learn how to do it here: https://hardhat.org/migrate-from-waffle Error HH8: There's one or more errors in your config file:

enter code here   * Invalid value undefined for HardhatConfig.networks.mumbai.url - Expected a value of type string.
  • Invalid account: #0 for network: mumbai - Expected string, received undefined

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

This error occur due to dotenv package and same time we forgot to take .env file on same directory.

0

✅ Ansewer

your .env file has an issue. I think just declare your key values with = instead of :

for example in .env file

KEY = value

wrong way is KEY : value

0

Same problem hare,


  * Invalid value undefined for HardhatConfig.networks.goerli.url - Expected a value of type string.
  * Invalid account: #0 for network: goerli - Expected string, received undefined

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

I solve this problem by adding require("dotenv").config() at the beginning of the hardhat.config file. Here

goerli: {
      url: process.env.RPC,
      accounts:[process.env.PRIVATE_KEY],
    },

Here process.env.RPC and process.env.PRIVATE_KEY is not recognized in your file without "dotenv" module.

0

Just to add a little to this question, I had the same error but my problem was that I was naming the node endpoint on the .env as GOERLI="https://....." instead of GOERLI_URL="https://.....".

Once changed everything worked ok.

grenos
  • 101
  • 1
0

Add goerli after networks and before url.

module.exports = {
  solidity: "0.8.4",
  networks: {
    goerli:{
      url: process.env.STAGING_ALCHEMY_KEY,
      accounts: [process.env.PRIVATE_KEY],
    }
  },
};
vishal
  • 23
  • 5
0

Error HH8: There's one or more errors in your config file:

  • Invalid value undefined for HardhatConfig.networks.goerli.url - Expected a value of type string.
  • Invalid account: #0 for network: goerli - Expected string, received undefined

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

if you get this kind of error then you just need to edit your env ...

all values have in string "example_url","Example_Api","Example_Privatekey"

0

Make sure you are importing dotenv module in your hardhat config file. If not, try to add the following command at the top of hardhat.config.js file:

require("dotenv").config();