Stack Exchange
How can I fix this JsonRpcProvider error?
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);
  });


Be careful to not share any private key

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

You should remove "providers" and directly use "ethers.JsonRpcProvider" to execute it.
const provider = new ethers.JsonRpcProvider("http://0.0.0.0:7545");


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