ethers.js: getNetwork returns "could not detect network" when running as a Cloudflare Worker

I’m seeing a strange issue only when I use Ethers as a Cloudflare worker. I can create the provider, but then i’m getting a noNetwork bug when it runs:

export async function handleRequest(request) {
  const provider = new ethers.providers.JsonRpcProvider("https://cloudflare-eth.com");
  let n = await provider.getNetwork()

results in the following:

Uncaught (in promise)
Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.4.1)
    at d.makeError (worker.js:2:161713)
    at d.throwError (worker.js:2:161833)
    at E.<anonymous> (worker.js:2:239261)
    at Generator.throw (<anonymous>)
    at a (worker.js:2:232620)
Uncaught (in response)
Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.4.1)

This only happens when running as a Cloudflare worker. When running in the browser or repl this code works fine.

I’ve created a branch to minimally reproduce this bug here: https://github.com/fafrd/ens-reverse-lookup/tree/ethers-bug. See handleRequest.js.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 19 (3 by maintainers)

Most upvoted comments

Yes, this works no problem. As a temporary workaround I implemented my own class which uses the underlying ethers.utils.Interface to construct requests, as I only need a handful of methods (gist)

Here is the base templates I used, very minimal webpack setup that listens for two routes. https://github.com/cloudflare/worker-template-router

It looks like there a similar reports in #1834 and #1886

I get the same result when I use my own node.

If the node is offline or not reachable for some reason then you’ll get the error could not detect network.

If you’re still facing this issue, can you try if this runs or what error do you get?

const result = await provider.send('eth_chainId', []);