JavaScript – Fix TypeError: Cannot Read Properties of Undefined ‘JsonRpcProvider’ in Ethers.js

ethers.jsjavascriptjson-rpcrpc

const ethers = require("ethers");

const provider = new ethers.providers.JsonRpcProvider('https://mainnet.infura.io/v3/MYKEYISHERE')

web3.eth.getBlockNumber().then(console.log)

Error:

const provider = new ethers.providers.JsonRpcProvider(`https://mainnet.infura.io/v3/MYKEYISHERE`)
                                      ^

TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider')

Best Answer

Look at this post.

EthersJS Listening to events with NodeJS Strange Errors

Had the same problem few days ago, turned out to be problem with a beta release of ethers.js.

The fix for me was to downgrade "ethers" to version 5.4 in the config.json, then remove node_modules folder and lastly npm install to install ethers again.

Related Topic