TypeError in Hardhat – Fixing TypeError: ethers.getSigners is not a function in Hardhat Tutorial

ethers.jshardhat

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

Best Answer

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.
Related Topic