In hardhat, I want to access a contract outside my project in my tests.

In my hardhat.config.js I am able to reference external contracts no problem with an ABI and a contract address, like so:

const tkn1 = new ethers.Contract(ADDRESS, ABI, signer)

However, in my test files, I am getting ethers is not defined when trying to do the same thing. Where am I going wrong?

5 Answers 5


I have been having the same issue. First make sure your hardhat.config.js file has this line: require("@nomiclabs/hardhat-waffle");

I fixed it by adding this to the top of the test file:

const { ethers } = require("hardhat");

But before you try that, see if your tests still run as expected. Someone told me its a linting error which made sense because my tests still ran and passed even though ethers was not defined. If this is the case, we will have to configure the linter to accept ethers as a variable. Still looking into all of it.

  • 2
    This is the one. A little unintuitive but works all the best. Mar 10, 2021 at 10:09
  • Please check the tip in this section. hardhat.org/getting-started/#quick-start
    – Bi Wu
    Dec 12, 2021 at 20:09
  • Sorry for the bump, but when I add these lines to my React app, I get many more errors about Module not found: Error: Can't resolve 'path' in XXXXXXX . My full post is here. Thanks!
    – That_Coder
    Mar 19, 2023 at 1:30

I faced the same problem. I didn't know I had to install ethers besides hardhat-ethers. So I installed ethers by running the following command:

npm install --save-dev ethers

And added the following line to the top of the test file:

const { ethers } = require("hardhat");

It works well now.


This pattern is recommended in the official hardhat documentation and it has always solved this issue for me.
Install @nomicfoundation/hardhat-toolbox with;
npm install --save-dev @nomicfoundation/hardhat-toolbox

Then add it to your hardhat.config.js like so;


/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.17",


In addition to the accepted answer, ensure that hardhat.config.js is included in your tsconfig.json.


I encountered a similar issue. My solution was to move from windows to WSL2 ubuntu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.