11
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')

12 Answers 12

13

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.

2
12

The newer version of Ethers seems to be unstable ethers 6.0.2... To solve this error downgrade your ethers version to 5.7.2 by going to your package.json

"ethers": "5.7.2"

now on your cli use npm install [email protected] or yarn add [email protected] depending on your package manager and that should solve your error.

1
  • Worked like a charm thank you Jul 4 at 18:30
7

I am using ethers version 6.2.3, You can directly import it from ethers package.

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

For more info refer to this link https://github.com/ethers-io/ethers.js/issues/3924.

Entire code I am using to deploy contract locally using ethers on Ganache

//importing thers and JsonRpcProvider
const { ethers, JsonRpcProvider } = require('ethers');
const fs = require('fs-extra');
async function main() {
  // Json Rpc Provider - Connecting to local blockchain
  const provider = new JsonRpcProvider('address'); // add your rpc server url from Ganache

  // Connect to wallet to sign transactions
  const wallet = new ethers.Wallet(
    'Add your private key here',
    provider
  );

  //Read ABI which you get post compilation using solc
  const abi = fs.readFileSync('./fileName.abi', 'utf8');
  
  //Read Binary which you get post compilation using solc
  const binary = fs.readFileSync(
    './fileName.bin',
    'utf8'
  );

  //Create Contract factory object to deploy
  const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
  console.log('Deploying, Please wait...');
  
  // Deploy and you are good to go
  const contract = await contractFactory.deploy();
  console.log(contract);
}

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

Response

Deploying, Please wait...
BaseContract {
  target: '0x35aD58985b61f634A8108A7ffcF6D438ab701ffB',
  interface: Interface {
    fragments: [ [FunctionFragment], [FunctionFragment], [FunctionFragment] ],
    deploy: ConstructorFragment {
      type: 'constructor',
      inputs: [],
      payable: false,
      gas: null
    },
    fallback: null,
    receive: false
  },
  runner: Wallet {
    provider: JsonRpcProvider {},
    address: '0x8EDA89abFAB838dc350c12fA8b6D9Ad630380B2f'
  },
  filters: {},
  fallback: null,
  [Symbol(_ethersInternal_contract)]: {}
}
1
  • One more thing to mention ethers version 6 is in Beta so you can downgrade to version 5 as well. Mar 28 at 20:32
2

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 package.json, then remove node_modules folder and lastly npm install to install ethers again.

1

2

I also faced the same issue, when I changed the version it worked adequately. change the version (6.0.3 to 5.7.2). The latest version is not stable.

1

I had the same problem, the problem is in the version of the ethers, so I changed the version of ethers to ^5.7.2 in package.json Then I deleted the whole folder node_modules and again installed all components

1

There are some problems in the latest ether.js version. Change the ether version to 5.4.0 in the package.json file then delete node modules and reinstall it using npm install.

1

As mentioned in the hardhat-ethers plugin docs you must install 'ethers@^5.0.0' version and it resolves the issue.

1
  • thank you for mentioning the source of information. I was wondering why version 6.2.3 is not working.
    – oldpride
    Apr 6 at 2:58
0

I think you will have to import JsonRpc provider at the beginning.

const { JsonRpcProvider } = require("ethers");
1
  • This didnt help.
    – Blissful
    Mar 9 at 8:26
0

For this error

Cannot read properties of undefined (reading 'JsonRpcProvider')

run this command to solve this issue;

npm install --save-dev ethers@^5

or

yarn add --dev ethers@^5
0

I was facing a similar issue yesterday, what I did was to uninstall ethers and reinstall a lower version, particularly from 6.8 to 5.4.

-1

simple:

npm uninstall ethers

npm install --save-dev @nomiclabs/hardhat-ethers 'ethers@^5.0.0'

In hardhat.config.js

require("@nomiclabs/hardhat-ethers");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.