ethers.js: JsonRpcProvider failed to detect network and cannot start up

Ethers Version

6.7.1

Search Terms

react native, ethers

Describe the Problem

I encountered the error ‘JsonRpcProvider failed to detect the network and cannot start up; retry in 1s.’ This may be due to an incorrect URL or the Ethereum node not being started when using JsonRpcProvider in my React Native app with version 0.71.5.

Code Snippet

getProvider(): ethers.JsonRpcProvider {
    if (this.#provider) {
      return this.#provider;
    }
    const config = ETH_NETWORK_CONFIG[this.#currentNetwork];
      if (!config) {
          return null;
      }
    this.#provider = new ethers.JsonRpcProvider(config.rpc);
      this.#provider.send("eth_chainId", []).then(number => {
          console.log(number)
      })
    return this.#provider;
  }

Contract ABI

No response

Errors

LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)
 LOG  JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)

Environment

No response

Environment (Other)

No response

About this issue

  • Original URL
  • State: open
  • Created 9 months ago
  • Reactions: 3
  • Comments: 16 (2 by maintainers)

Most upvoted comments

When using Alchemy (or anything that doesn’t change networks) you will save a lot of requests by passing in a Network and the option { staticNetwork: true }. This will make it skip querying for the network. It will also mean you will get the underlying error more readily.

provider = new JsonRpcProvider(url, Network.from(blah), { staticNetwork: true })

I just stumbled upon this myself. I see the implementation detects the network indefinitely.

We use ethers in a BE only app and this caused a quite serious problem. Essentially, we run a function periodically (using setInterval) and at the start of the function we initialize the provider. One of the RPCs was broken and ethers tried to indefinitely retry network detection each time we ran the function 😕 until it crashed the service.

I fixed it by creating the provider more carefully by calling provider._detectNetwork() and in case it failed I call provider.destroy() to destroy the provider. I am not a fan of the console.log either, because we use a dedicated logger and can’t turn off this message.

Could this logic be changed ?

As a note, calling provider._detectNetwork() verifies that the RPC is working, but ethers will still run the problematic code. To avoid it completely, you can do this:

  const network = new ethers.Network(chainName, chainId);
  const provider = new ethers.JsonRpcProvider(providerUrl, network, {
    staticNetwork: network,
  });

which makes this condition return false so that it’s not run at-all.

I was having the same issue , i was making voting Dapp , I made frontend and when it was ready I made a copy of the frontend and saved it. Now I edited and installed and uninstalled to many packages in my full stack project but so at the end , I deleted all the packages except ether.js package , even if i tried to make a simple “Connect Wallet” button using ether.js it was not working it was giving me this error in the console" JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)" , Now my copy was untouched that i made of frontend , there i only installed ether.js using “npm i --save -ethers” in my client folder and when I made “Connect wallet” button using ether.js it was working , in short i think that some of the packages before were clashing or maybe there is something in cache in the folder which was giving an error but when i started clean only installing the package i need it did work .

This was my connectWallet function that was called by button "Connect Wallet " in both the cases which was same for both the cases

 const connectWallet = async ()=>{
      if(typeof window != "undefined" && typeof window.ethereum != "undefined"){

        const provider =  new ethers.BrowserProvider(window.ethereum)
        await provider.send("eth_requestAccounts", []);
        const signer  =  await provider.getSigner()
        // const contract =  new ethers.Contract(contractAdd,Abi.abi,signer) 
        //Uncomment above if you are using contract or making a dapp
        console.log(signer.address)
        alert("Metamask connected");
        setWalletConnected(true)
      }
      else{
        console.log("Please install metamask")
      }

    
}