I am following this tutorial https://hardhat.org/tutorial/testing-contracts.html and I get this unexpected error:

const { expect } = require("chai");
const { ethers } = require("ethers");

describe("Token contract", function() {
  it("Deployment should assign the total supply of tokens to the owner", async function() {
    const [owner] = await ethers.getSigners();
    const Token = await ethers.getContractFactory("Token");

    const hardhatToken = await Token.deploy();

    const ownerBalance = await hardhatToken.balanceOf(owner.address);
    expect(await hardhatToken.totalSupply()).to.equal(ownerBalance);

$ npx hardhat test

TypeError: ethers.getContractFactory is not a function

2 Answers 2


Two things happened:

  1. require("@nomiclabs/hardhat-waffle"); was missing in hardhat.config.js (as mentionned at the end of https://hardhat.org/tutorial/creating-a-new-hardhat-project.html)
  2. const { ethers } = require("ethers"); should be replaced by const { ethers } = require("hardhat"); or removed as it is available in the global scope.

As per MadBreaks comment, Hardhat is better organised to keep things internal (ie. provide defaults) including not needing to bring in ...-waffle yourself.

You should just require this in the hardhat.config.[js|ts] as per https://hardhat.org/hardhat-runner/docs/guides/project-setup:


import "@nomicfoundation/hardhat-toolbox";



And these will bring in:


import "@nomicfoundation/hardhat-chai-matchers";
import "@nomicfoundation/hardhat-ethers";
import "@nomicfoundation/hardhat-verify";
import "@typechain/hardhat";
import "hardhat-gas-reporter";
import "solidity-coverage";



And hardhat-ethers is what you need. Specifically it is (type-extensions.d.ts):


import type { ethers } from "ethers";
import type { FactoryOptions as FactoryOptionsT, getContractFactory as getContractFactoryT, HardhatEthersHelpers, Libraries as LibrariesT } from "../types";
import "hardhat/types/runtime";
declare module "hardhat/types/runtime" {
    interface HardhatRuntimeEnvironment {
        ethers: typeof ethers & HardhatEthersHelpers;
    type Libraries = LibrariesT;
    type FactoryOptions = FactoryOptionsT;
    type getContractFactory = typeof getContractFactoryT;
//# sourceMappingURL=type-extensions.d.ts.map

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.