Error using Hardhat: TypeError:ethers.getContractFactory is not a function

I know ethers.getContractFactory is a function so am wondering what am missing. Maybe importing something?

:computer: Environment
Truffle v5.1.67 (core: 5.1.67)
Solidity v0.5.16 (solc-js)
Node v12.19.0
Web3.js v1.2.9


:1234: Code to reproduce
Here is the first part of the code, everything seems ok, no?

const { expect } = require('chai');

const { ethers } = require('ethers');

describe('Token contract', () => {

    let Token, token, owner, addr1, addr2;

    beforeEach(async () => {

        Token = await ethers.getContractFactory("Token");

        token = await Token.deploy();

        [owner, addr1, addr2, _] = await ethers.getSigners();


    describe('Deployment', () => {

        it('Should set the right owner', async () => {

            expect(await token.owner()).to.equal(owner.address);


        it('Should assign the total supply of tokens to the owner', async () => {

            const ownerBalance = await token.balanceOf(owner.address);

            expect(await token.totalSupply()).to.equal(ownerBalance);


1 Like

I think you should remove this line

const { ethers } = require('ethers');

thanks, can you explain why this line needs to be removed?

const { ethers } = require("ethers"); should be replaced by const { ethers } = require("hardhat"); or removed as it is available in the global scope.


Hi, welcome! :wave:

I think it is available in the global scope.

1 Like

This worked for me…

const hre = require("hardhat");
const {
} = require('chai');

describe("NFT", () => {
  it("It should deploy the contract, mint a token, and resolve to the right URI", async () => {
    const NFT = await hre.ethers.getContractFactory("UkuNFT");
    const nft = await NFT.deploy();
    const URI = "ipfs://QmWJBNeQAm9Rh4YaW8GFRnSgwa4dN889VKm9poc2DQPBkv";
    await nft.deployed();
    await"0x09.............................", URI)
    expect(await nft.tokenURI(1)).to.equal(URI)

I got this same issue. This is because when you use “ethers” vscode automatically imports it into the file, which disrupts the global declaration.

1 Like

A post was merged into an existing topic: A contract that can't estimate gas while selling, but can buy Without issue

Amazing solution. Thanks alot.

I've tried these solution but they didn't work for me.
Is there any other solution for this 'TypeError: ethers.getContractFactory(...).deploy is not a function'?

Thanks in advance.

Hi, welcome to the community! :wave:

Maybe you can paste your test code, so then we can have a look what is wrong.

const { expect } = require("chai");
const { ethers } = require("hardhat");
const {
  BN, // Big Number support
  constants, // Common constants, like the zero address and largest integers
  expectEvent, // Assertions for emitted events
  expectRevert, // Assertions for transactions that should fail
} = require("@openzeppelin/test-helpers");

async function fetchContractFactory() {
  const contractFactory = await ethers.getContractFactory("example");


describe("example", function () {
  let example, bob;

  before(async function () {
    bob = await ethers.getVerifiedContractAt(
    console.log("Should be able to read the correct address " + bob.address);
    console.log("About to deploy example contract");

    example = await ethers.getContractFactory("example").deploy(bob.address);
    console.log(`example contract deployed at ${example.address}`);

  it("bb", async function () {
    await example.arrival();

    const result = await example.bob.fill("roll", 0);


This is the code, what do you think?

Thanks for the welcome.

So the error message is at this line,
const contractFactory = await ethers.getContractFactory("example");
or at this line,
example = await ethers.getContractFactory("example").deploy(bob.address);

The error is at this line:

example = await ethers.getContractFactory("example").deploy(bob.address);

Any solution?

Thanks in advance

I think you can change your writing,

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

describe("Greeter", function () {
  it("Should return the new greeting once it's changed", async function () {
    const Greeter = await ethers.getContractFactory("Greeter");
    const greeter = await Greeter.deploy("Hello, world!");
    await greeter.deployed();

    expect(await greeter.greet()).to.equal("Hello, world!");

    const setGreetingTx = await greeter.setGreeting("Hola, mundo!");

    // wait until the transaction is mined
    await setGreetingTx.wait();

    expect(await greeter.greet()).to.equal("Hola, mundo!");

This is a simple test case, it works well.