【问题标题】:Hardhat compile error with API_URL and Private Key import使用 API_URL 和私钥导入的 Hardhat 编译错误
【发布时间】:2021-11-30 04:03:18
【问题描述】:

追随基本的NFT tutorial,我不得不说,我在编程方面有点菜鸟。我面临的问题是我的 hardhat.config.js 文件需要一个 API_KEY 和一个私钥,它应该从 process.env 文件中导入:

API_URL = "https://ethropsten.alchemyapi.io/v2/UkW3oySI7WxvFwDwopQHPOHajHaWFZFv"
PRIVATE_KEY = "8d33c2613cb63d0dc6305e57..."

hardhad 配置文件如下所示:

* @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}`]
    }
  },
} 

但是每当我尝试编译它并在我的 deploy.js 文件中运行它时,我都会收到一条错误消息,基本上告诉我,导入是不可能的,看起来像这样:


  * Invalid value undefined for HardhatConfig.networks.ropsten.url - Expected a value of type string.
  * Invalid value {"accounts":["0xundefined"]} for HardhatConfig.networks.ropsten - Expected a value of type HttpNetworkConfig.
  
To learn more about Hardhat's configuration, please go to https://hardhat.org/config/

For more info go to https://hardhat.org/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._compile (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._compile (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 

我首先想到,我的 .env 文件不在正确的位置或语法不正确,但在尝试了我能想到的一切之后,我仍然收到相同的错误消息。任何帮助表示赞赏。

【问题讨论】:

    标签: javascript solidity nft hardhat


    【解决方案1】:

    我遇到了这个问题,因为我在环境变量前面添加了“导出”。 删除“导出”后,我就可以从安全帽配置中访问它们

    【讨论】:

    【解决方案2】:

    我通过在hardhat.config.js 文件中使用它解决了同样的问题:

    require('dotenv').config({path:__dirname+'/.env'})
    

    代替:

    require('dotenv').config()
    

    欲了解更多信息,您可以访问 dotenv file is not loading environment variables

    【讨论】:

      猜你喜欢
      • 2021-11-17
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多