PatrickAlphaC / ethers-simple-storage-fcc

90 stars 128 forks source link

Resolving Error: TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider') when try do deploy #88

Open Maximesol opened 1 year ago

Maximesol commented 1 year ago

Problem :

When I was trying to use the ethers module in my Solidity project to interact with my contract deployed on Ganache, I encountered an error: TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider').

My code looked like this, like in the course :

const ethers = require("ethers");

const provider = new ethers.providers.JsonRpcProvider("address of your local ganache BC");

Error Resolution : I realized that the error was due to how I was importing and using the ethers module. In the original code, I imported the entire ethers module and assigned it to the constant ethers. However, when I tried to access JsonRpcProvider with ethers.providers.JsonRpcProvider, it wasn't found because it doesn't exist at that location in the ethers module's structure.

To resolve the error, I used a JavaScript feature called destructuring assignment to extract ethers and JsonRpcProvider directly from the ethers module. Here's what the corrected code looks like:

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

const provider = new JsonRpcProvider("address of your local ganache BC");

This worked and my contract was successfully deployed on Ganache. I hope this can help someone else who encounters the same error. I'm a newbie so if I've said something stupid don't hesitate to correct me.

vulpes231 commented 1 year ago

I encountered the same problem, and after searching online, I discovered that the solution was to execute the following command in the command line: npm install ethers@^5.7.2. This command effectively downgraded the ethers library to version 5.7.2, which also resolved the issue for me.

ArtemYo8283 commented 1 year ago

you can also use

const provider = new ethers.JsonRpcProvider("address of your local ganache BC");