4

I wanted to add hardhat to a project that uses ES modules. Hardhat complains that I can't use require in the hardhat.config.js file, so I renamed it to import, but it still won't compile with the following error:

require() of ES modules is not supported.

There isn't a place where require is being called (I have replaced it with import), but it says otherwise. Any solution?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Abdi mussa
  • 171
  • 11

1 Answers1

0

I have "type": "module", in my package.json

I am using the "hardhat": "^2.12.0-esm.1" package. It's a new package that was specially written to support ESM.

In my test, if I need some HH function, I do something like:

import pkg from 'hardhat'
const { ethers } = pkg

My hardhat.config.js is renamed to hardhat.config.cjs but the inside is unchanged and has lots of requires in it, and they work, even though my package.json has type: "module".

In my scripts, I have lines like:

async function main() {
  const [deployer] = await ethers.getSigners()

but I don't have any import nor require at the beginning. I guess calling hardhat run script/my_script.js does the import magic.

MetaZebre
  • 769
  • 8
  • 10