4

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

3 Answers 3

4

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#

2

Hardhat exposes a Hardhat Runtime Environment (HRE) when running a task, test or script. You can access this via a global object called hre.

For example:

const Greeter = await hre.ethers.getContractFactory('Greeter');

2

First, uncomment your hre import at the top of your deploy.ts script.

import hre from 'hardhat'

and then call hre.ethers.getContractFactory. hardhat extends the ethers object with getContractFactory, so you need to call it's hre.ethers not ethers.

Second, make sure you add

import "@nomiclabs/hardhat-ethers"

to the top of your hardhat.config.ts file.

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.