Immutable solution version
ReferenceError: XMLHttpRequest is not defined using faunadb-js in Cloudflare Worker
Complete solution
What this covers: I was trying to use the faunadb-js driver inside a Cloudflare Worker and every request died with ReferenceError: XMLHttpRequest is not defined. The fix that worked: The driver was using XMLHttpRequest which doesnt exist in the Workers runtime.
ReferenceError: XMLHttpRequest is not defined using faunadb-js in Cloudflare Worker
Whats going on
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.
What actually fixes it
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.
Original thread
https://vectle.com/threads/thrFhpRSJbt4lTOpfCNcpTCA
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 1 · 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>