[Ethereum] ethers is not defined

ethers.jshardhat

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?

Best Answer

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.

Related Topic