Module '"hardhat"' has no exported member 'ethers'

Viewed 664

When I want to import ethers from hardhat it throws the error that I mentioned in the title here is a complete version

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

Although I used it the same way in my previous projects and seen everyone doing the same

4 Answers

I had to add these two lines to tsconfig.json

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

Here is how the issue was resolved on my side:

  1. I added '@nomiclabs/hardhat-ethers' dependency by running npm install --save-dev @nomiclabs/hardhat-ethers
  2. import @nomiclabs/hardhat-ethers
  3. ethers now becomes available from 'hre' as it ethers is sort of injected into hre: so the code snippet:

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')
  })
})

Turns out @nomiclabs/hardhat-ethers exists as separated plugin that is 'injected' into 'hre' named import from 'hardhat' package.

I hope this helps, the other two answers did not work for me.

Happy coding, WAGMI!

You need to create a tsconfig.json in your root project containing

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