Skip to content
Advertisement

Importing ethers via Hardhat fails despite official testing documentation

According to the official testing documentation for Hardhat, ethers should be available implicitly within the global scope; however, it can optionally be required explicitly, like so:

JavaScript

This fails for my local project.

My package manifest seems to include the correct dependencies:

JavaScript

My unit tests file seems to match the worked example in the Hardhat documentation also:

JavaScript

Despite this, running the tests fails with:

JavaScript

How do I fix this?

Advertisement

Answer

Add the require in your hardhat.config.js

JavaScript

And remove this line from your test file:

JavaScript

Then, you can use ethers in your tests. Hardhat looks in the config first before running tests. If you have required a package that includes ethers you can use it in the global scope.

Advertisement