模块“hardhat”没有导出成员“ethers”

Yah*_*var 8 typescript reactjs hardhat

当我想ethers从中导入时hardhat,会抛出我在标题中提到的错误,这里是完整版本

 - error TS2305: Module '"hardhat"' has no exported member 'ethers'.
 
     2 import { ethers } from "hardhat";
                ~~~~~~
Run Code Online (Sandbox Code Playgroud)

虽然我在之前的项目中以同样的方式使用它并且看到每个人都这样做

Yah*_*var 11

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

  "include": ["./src", "./scripts"],//only the "scripts" part.
  "files": ["./hardhat.config.ts"]
Run Code Online (Sandbox Code Playgroud)


nmu*_*gor 7

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

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

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')
  })
})
Run Code Online (Sandbox Code Playgroud)

事实证明,@nomiclabs/hardhat-ethers作为单独的插件存在,该插件被“注入”到“hre”中,名为从“hardhat”包导入。

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

快乐编码,WAGMI!