【问题标题】:Module '"hardhat"' has no exported member 'ethers'模块 \'\"hardhat\"\' 没有导出成员 \'ethers\'
【发布时间】:2022-08-16 17:52:03
【问题描述】:

当我想从hardhat 导入ethers 时,它会抛出我在标题中提到的错误here is a complete version

 - error TS2305: Module \'\"hardhat\"\' has no exported member \'ethers\'.
 
     2 import { ethers } from \"hardhat\";
                ~~~~~~

尽管我在之前的项目中使用相同的方式并且看到每个人都在做同样的事情

    标签: reactjs typescript hardhat


    【解决方案1】:

    我必须将这两行添加到tsconfig.json

      "include": ["./test", "./src", "./scripts"],//only the "scripts" part.
      "files": ["./hardhat.config.ts"]
    

    【讨论】:

      【解决方案2】:

      他们可能不会明确导出以太币。在文档中,它显示了以下列方式使用醚:

      import hre from "hardhat";
      
      const Lock = await hre.ethers.getContractFactory("Lock");
      

      https://hardhat.org/hardhat-runner/docs/guides/test-contracts

      【讨论】:

      • 我认为你也应该在那里有 hre.ethers 。 hre.((etheres)) 没有多大意义
      • 我知道。我只是添加了括号来加粗它引发错误的部分。
      • 你是进口的吗?你添加依赖了吗?你在正确的版本吗?
      【解决方案3】:

      您需要在您的根项目中创建一个tsconfig.json,其中包含

      {
        "compilerOptions": {
          "target": "es2020",
          "module": "commonjs",
          "esModuleInterop": true,
          "forceConsistentCasingInFileNames": true,
          "strict": true,
          "skipLibCheck": true
        }
      }
      

      【讨论】:

        【解决方案4】:

        以下是我这边解决问题的方法:

        1. 我通过运行npm install --save-dev @nomiclabs/hardhat-ethers 添加了“@nomiclabs/hardhat-ethers”依赖项
        2. import @nomiclabs/hardhat-ethers
        3. ethers 现在可以从“hre”获得,因为它 ethers 有点注入到 hre 中: 所以代码sn-p:

          import { expect } from 'chai'
          import hre from 'hardhat'
          import {Contract} from 'ethers'
          import '@nomiclabs/hardhat-ethers'
          
          describe('SocialRecovery', function () {
            let fooBar: Contract
            beforeEach(async function () {
              const foobar = await hre.ethers.getContractFactory('FooBar')
            })
          })

          原来@nomiclabs/hardhat-ethers 作为单独的插件存在,它被“注入”到名为“hardhat”包的“hre”中。

          我希望这会有所帮助,其他两个答案对我不起作用。

          快乐编码, 瓦格米!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-20
          • 2017-05-07
          • 2017-08-18
          • 1970-01-01
          • 2017-09-09
          • 2022-01-12
          • 2021-12-19
          相关资源
          最近更新 更多