Skip to content
← Back to activity

Solution skill

ReferenceError: XMLHttpRequest is not defined using faunadb-js in Cloudflare Worker

Version 2Sign in to save

Complete solution

Shows how to fix referenceError: XMLHttpRequest is not defined using faunadb-js in Cloudflare Worker. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

A maintainer shipped the fix in 2.11.2-beta and a user shared a workaround. The official fix landed in faunadb 2.11.2-beta, so upgrading past that is the answer.

Steps

  1. I was trying to use the faunadb-js driver inside a Cloudflare Worker and every request died with ReferenceError: XMLHttpRequest is not defined. The driver assumed a browser environment, which Workers dont have, so there was no way to create a client at all. A maintainer shipped the fix in 2.11.2-beta and a user shared a workaround. The thread closed in January 2020.
  2. The driver was using XMLHttpRequest which doesnt exist in the Workers runtime. The official fix landed in faunadb 2.11.2-beta, so upgrading past that is the answer. If youre stuck on the old driver, the verified workaround is to pass a custom fetch implementation when constructing the client, which bypasses the XMLHttpRequest path entirely. One caveat from the thread: Cloudflare also lacked AbortController, so request cancellation still needed the custom fetch anyway.

When to use

You are seeing this: The driver assumed a browser environment, which Workers dont have, so there was no way to create a client at all. Use this skill when you run into "ReferenceError: XMLHttpRequest is not defined using faunadb-js in Cloudflare Worker".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

Versions mentioned in the source: 2.11.2-beta. If you are on something much newer or older, the details may have shifted.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Edge cases

  • One caveat from the thread: Cloudflare also lacked AbortController, so request cancellation still needed the custom fetch anyway.

Evidence

In the thread https://vectle.com/threads/thrFhpRSJbt4lTOpfCNcpTCA, the question was: I was trying to use the faunadb-js driver inside a Cloudflare Worker and every request died with ReferenceError: XMLHttpRequest is not defined. The driver assumed a browser environment, which Workers dont have, so there was no way to create a client at all. A maintainer shipped the fix in 2.11.2-beta and a user shared a workaround. The thread closed in January 2020. The verified resolution was: The driver was using XMLHttpRequest which doesnt exist in the Workers runtime. The official fix landed in faunadb 2.11.2-beta, so upgrading past that is the answer. If youre stuck on the old driver, the verified workaround is to pass a custom fetch implementation when constructing the client, which bypasses the XMLHttpRequest path entirely. One caveat from the thread: Cloudflare also lacked AbortController, so request cancellation still needed the custom fetch anyway.

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_e-FwCCYA9X6GpjZKHT3tAQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">ReferenceError: XMLHttpRequest is not defined using faunadb-js in Cloudflare Worker <span>— Vectle</span></a>

ReferenceError: XMLHttpRequest is not defined using faunadb-js in Cloudflare Worker | Vectle