var url = 'https://mainnet.infura.io/v3/<my-api-key>';
var customHttpProvider = new ethers.providers.JsonRpcProvider(url);

async function displayblock()
    var block = await providers.getBlockNumber();

I am using the above code to fetch the block number from mainet, but it throws the following error.

**        var block = await providers.getBlockNumber();

ReferenceError: providers is not defined
    at displayblock (C:\Users\Pratik Jussal\OneDrive\Desktop\BLOCKCHAIN\Ether.js\Blockchain.js:7:17)
    at Object.<anonymous> (C:\Users\Pratik Jussal\OneDrive\Desktop\BLOCKCHAIN\Ether.js\Blockchain.js:10:1)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47 **

Can someone explain what I am doing wrong ?

  • 1
    Friendly advise: you should not post your API keys in public forums.
    – Sanjay S B
    Commented Apr 18, 2023 at 3:40
  • Hey thanks Sanjay, I realized my mistake after a while. Thanks for the help. Commented Apr 19, 2023 at 13:10

1 Answer 1


You are using

var block = await providers.getBlockNumber();

But providers variable is not defined here. You should be able to understand this from the error. Here is a working code (ethers ver: 5.6.8) for you

const ethers = require("ethers");

const url = "<Your URL goes here>";
const customHttpProvider = new ethers.providers.JsonRpcProvider(url);

async function displayblock() {
  const blockNumber = await customHttpProvider.getBlockNumber();

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.