[Ethereum] Property ‘getContractFactory’ does not exist on type

hardhathardhat-deploy

Trying to run npx hardhat run --network ropsten scripts/deploy.ts

deploy.ts throws a type error


import { ethers } from "ethers";
// const hre = require("hardhat");

async function main() {

  // await hre.run('compile');

  const Greeter = await ethers.getContractFactory("Greeter")
  const greeter = await Greeter.deploy("Hello, Hardhat!")

  await greeter.deployed()

  console.log("Greeter deployed to:", greeter.address)
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error)
    process.exit(1)
  });

Property 'getContractFactory' does not exist on type 'typeof import("/home/ubuntu/demos/react-dapp/node_modules/ethers/lib/ethers")'.

Throws an error when hre gets removed from const Greeter = await hre.ethers.getContractFactory("Greeter") to await ethers.getContractFactory("Greeter")

source code: https://github.com/GoGetterMeme/react_dapp.git

Best Answer

ethers.getContractFactory is a one of the helpers that Hardhat added to the ethers object so you need to call hre first.

https://hardhat.org/plugins/nomiclabs-hardhat-ethers.html#helpers

Notice getContractFactory doesn't exist in the Ethers.js docs:

https://docs.ethers.io/v4/search.html?q=getContractFactory&check_keywords=yes&area=default#