1
TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider')
    at main (D:\ethers-simple-storage\deploy.js:9:41)
    at Object.<anonymous> (D:\ethers-simple-storage\deploy.js:26:1)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

And my code is:

const ethers = require("ethers");
const fs = require("fs-extra");

async function main() {
  const ethers = require("ethers");
  const fs = require("fs-extra");
  const provider = new ethers.providers.JsonRpcProvider("http://0.0.0.0:7545");

  const wallet = new ethers.wallet(
    "PRIVATEKEY",
    provider
  );
  const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf-8");
  const binary = fs.readFileSync(
    "./SimpleStorage_sol_SimpleStorage.bin",
    "utf-8"
  );
  const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
  console.log("Deploying, please wait...");
  const contract = await contractFactory.deploy(); //Stop here wait for contract to deploy....
  console.log(contract);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
1
  • Be careful to not share any private key
    – Adam Boudj
    Commented Apr 12, 2023 at 23:10

3 Answers 3

2

You accidentally required the ethers and fs modules twice in your code.

 const ethers = require("ethers");
  const fs = require("fs-extra");

Remove the duplicate lines inside the main function to fix the issue.

Additionally, there are a lot of breaking changes in ethers v6, which is still in beta; I recommend you to use v5.4.x for the moment

1

You should remove "providers" and directly use "ethers.JsonRpcProvider" to execute it.

const provider = new ethers.JsonRpcProvider("http://0.0.0.0:7545");
0

Basically for me, it was an issue with ethers. Downgrading to v5.4 fixed it.

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.