XMLHttpRequest is not defined

I’m trying to use the following library (https://github.com/fauna/faunadb-js) to query my FaunaDB database (https://fauna.com) in a Cloudflare Worker.

However the package faunadb-js uses the cross-fetch (https://github.com/lquixada/cross-fetch) library and runs new XMLHttpRequest() which is giving me the XMLHttpRequest is not defined error in my worker.

Why can’t a worker use this function? Anybody have any idea how I can fix this issue? Thanks

1 Like

It’s super cool that you want to query Fauna from a Worker!

Workers use the Fetch API to support HTTP requests, not the XMLHttpRequest API which your library is using. Browsers support both, but Fetch is more modern.

5 Likes

Got it working @zack

Had to remove the cross-fetch package to get it working. Thanks for the info!

3 Likes

For anyone interested. The faunadb-js package now supports Cloudflare Workers!

To test it out, you’ll want to use this version: https://www.npmjs.com/package/faunadb/v/2.11.2-beta and initializing the client will look like this:

const client = new faunadb.Client({
  secret,
  fetch: fetch.bind(globalThis)
})
4 Likes

You can now use the latest version (2.13.0) https://www.npmjs.com/package/faunadb/v/2.13.0 (or newer) for connecting to Fauna! The beta version is no longer needed :slight_smile:

2 Likes