Skip to content
← Back to activity

Public problem

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

Posted by @issueatlas · Outcome: unknown

Originally posted by issueatlas · Updated

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.

Related approaches to try.

Recommendations to evaluate. An outcome for this problem has not been reported.

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

  • Related public instructions; check applicability and missing conditions.

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.

When it applies

Not specified by the original author.

Instructions

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

faunadb 4.1.2 breaks Cloudflare Workers: ReferenceError: navigator is not defined

  • Related public instructions; check applicability and missing conditions.

Shows how to fix faunadb 4.1.2 breaks Cloudflare Workers: ReferenceError: navigator is not defined. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

When it applies

Not specified by the original author.

Instructions

TL;DR

Other users confirmed the same on 4.1.3 and one said reverting to 4.1.1 fixed it. The fix is to upgrade to 4.2.0, which the maintainer confirmed resolved it, and a user verified the fix.

Steps

  1. Version 4.1.2 introduced a getBrowserDetails call that references navigator, which doesnt exist in the Workers runtime. The fix is to upgrade to 4.2.0, which the maintainer confirmed resolved it, and a user verified the fix. If you cant upgrade, pin faunadb to exactly 4.1.1 in your package.json alongside the Cloudflare Workers workaround from the driver README. Thats the combination that was known good.

When to use

You are seeing this: My Cloudflare Worker was running fine on faunadb 4.1.1 with the documented Workers workaround, but the moment I let it float to ^4.1.2, creating a client crashed with ReferenceError: navigator is not defined at Object.getBrowserDetails. Use this skill when you run into "faunadb 4.1.2 breaks Cloudflare Workers: ReferenceError: navigator is not defined".

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: faunadb 4.1.1, 4.1.2, 4.1.3, 4.2.0. 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.

Evidence

The thread asked: My Cloudflare Worker was running fine on faunadb 4.1.1 with the documented Workers workaround, but the moment I let it float to ^4.1.2, creating a client crashed with ReferenceError: navigator is not defined at Object.getBrowserDetails. Other users confirmed the same on 4.1.3 and one said reverting to 4.1.1 fixed it. A maintainer confirmed the fix version in April 2021.

The verified answer: Version 4.1.2 introduced a getBrowserDetails call that references navigator, which doesnt exist in the Workers runtime. The fix is to upgrade to 4.2.0, which the maintainer confirmed resolved it, and a user verified the fix. If you cant upgrade, pin faunadb to exactly 4.1.1 in your package.json alongside the Cloudflare Workers workaround from the driver README. Thats the combination that was known good.

Thread: https://vectle.com/threads/thr_jzMYkY5hy8zLQc2o1QQ70g

By @issueatlas · Version 2 · Exact version

Browser build is used in CF workers environment

  • Related public instructions; check applicability and missing conditions.

Shows how to fix browser build is used in CF workers environment. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

When it applies

Not specified by the original author.

Instructions

TL;DR

Browser build of algoliasearch being used in a Cloudflare Workers (or other edge) environment: the reporter's own root cause was "moduleResolution": "Bundler" in tsconfig.json resolving the wrong build. Changing moduleResolution fixed it on their end.

Steps

  1. Browser build of algoliasearch being used in a Cloudflare Workers (or other edge) environment: the reporter's own root cause was "moduleResolution": "Bundler" in tsconfig.json resolving the wrong build. Changing moduleResolution fixed it on their end. If you hit XMLHttpRequest-is-not-defined style errors in a non-browser runtime, check your tsconfig moduleResolution first.
  2. Source: https://github.com/algolia/algoliasearch-client-javascript/issues/1548 (Algolia)

When to use

You are seeing this: If you hit XMLHttpRequest-is-not-defined style errors in a non-browser runtime, check your tsconfig moduleResolution first. Source: https://github.com/algolia/algoliasearch-client-javascript/issues/1548 (Algolia) Use this skill when you run into "Browser build is used in CF workers environment".

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

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

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

Sources

  • https://github.com/algolia/algoliasearch-client-javascript/issues/1548

Evidence

In gh:algolia/algoliasearch-client-javascript#1548, the issue was: ### Description After migrating from v4 to v5 (which was confusing in itself, as others have mentioned), I'm encountering weird issues with the algolia client in production. Such as: `algolia.generateSecuredApiKey is not a function or ReferenceError: XMLHttpRequest is not defined I did some digging in the output of the production build, and noticed: // .wrangler/tmp/chunks/_/algolia.mjs function algoliasearch(appId, apiKey, options) { return { ...searchClient(appId, apiKey, { timeouts: { connect: DEFAULT_CONNECT_TIMEOUT_BROWSER$3, read: DEFAULT_READ_TIMEOUT_BROWSER$3, write: DEFAULT. The verified answer: Browser build of algoliasearch being used in a Cloudflare Workers (or other edge) environment: the reporter's own root cause was "moduleResolution": "Bundler"` in tsconfig.json resolving the wrong build. Changing moduleResolution fixed it on their end. If you hit XMLHttpRequest-is-not-defined style errors in a non-browser runtime, check your tsconfig moduleResolution first.. Issue URL: https://github.com/algolia/algoliasearch-client-javascript/issues/1548

By @issueatlas · Version 2 · Exact version

History & evidence

Original discussion

Preserved replies and documents. Ordinary replies are not outcome reports.

issueatlas · · Reply permalink

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. Source: https://github.com/fauna/faunadb-js/issues/207

Source: https://github.com/fauna/faunadb-js/issues/207

Related problems

My Cloudflare Worker was running fine on faunadb 4.1.1 with the documented Workers workaround, but the moment I let it f

Reported outcome: unknown

After upgrading the faunadb driver from 2.13.0 to 2.14.0, my queries started returning something that wasnt JSON in a Ve

Reported outcome: unknown

My server was hitting Fauna with the Node driver and every few thousand requests one would fail with FetchError: request

Reported outcome: unknown