0

I'm building a JavaScript application where I'm trying to interact with the Ethereum blockchain using ethers.js. However, I'm encountering an error stating that JsonRpcProvider is not found. This issue is preventing me from deploying a contract.

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

const fs = require("fs");

async function main() {
  const provider = new ethers.providers.JsonRpcProvider(
    "http://127.0.0.1:7545"
  );
  const wallet = new ethers.Wallet(
    "0xb6f68c91d6c08f1aa1b2dd8c87794ec09a7af7c776cca4eb1ce562f810ccce6a",
    provider
  );

  const abi = JSON.parse(
    fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf-8")
  );
  const bytecode =
    "0x" + fs.readFileSync("./SimpleStorage_sol_SimpleStorage.bin", "utf-8");

  const contractFactory = new ethers.ContractFactory(abi, bytecode, wallet);
  console.log("Deploying, please wait...");
  const contract = await contractFactory.deploy();

  console.log("Contract deployed to address:", contract.address);
}

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

Error Message:

TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider')
    at main (/path/to/your/file.js:5:41)
    at Object.<anonymous> (/path/to/your/file.js:26:1)
    at Module._compile (node:internal/modules/cjs/loader:1358:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
    at Module.load (node:internal/modules/cjs/loader:1208:32)
    at Module._load (node:internal/modules/cjs/loader:1024:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
    at node:internal/main/run_main_module:28:49

I've tried checking my imports and ensuring that ethers.js is properly installed in my project. I've also checked for any typos in the code and verified that the necessary dependencies are included in my package.json file.

I'm using yarn as my package manager, and my package.json file includes the necessary dependencies for ethers.js and fs. I've also ensured that the contract ABI and bytecode files exist in the specified paths.

{
  "name": "ethers-simple-storage-example",
  "version": "1.0.0",
  "license": "MIT",
  "dependencies": {
    "ethers": "^6.12.1",
    "fs": "^0.0.1-security",
    "solc": "^0.8.25"
  },
  "scripts": {
    "compile": "yarn solcjs --bin --abi --include-path node_modules/ --base-path . -o . SimpleStorage.sol"
  }
}

this error

2 Answers 2

0

Since you’re using ethers.js v6 (6.12.1), you’ve to access it like:

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

P.S., The way you’re currently doing is applicable to ethers.js v5.

2
  • that's work with V5 but Ganache is not detected reason is "could not detect network" in terminal Commented May 15 at 13:45
  • Is your Ganache node running? Commented May 15 at 17:02
0

Make sure your local Ethereum node (like Geth or Ganache) is running and listening on port 7545 as you are specifying the URL of your local Ethereum node in your program as "http://127.0.0.1:7545"

  • If you are using Ganache, it usually has a GUI which provides information on which port the node is running at
  • If you are using Geth, you should be able to see the console output indicating it's running and listening on port 7545.
1
  • that's work with ether V5 but Ganache is not detected reason is "could not detect network" in terminal and Ganache is not worke with windows priorly?? Commented May 15 at 13:47

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.