question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"XMLHttpRequest is not defined" on sveltekit endpoints when deployed on Vercel

See original GitHub issue

Bug report

Describe the bug

Using the client for auth or for querying the database in a sveltekit endpoint throw an error :

{
  data: null,
  error: ReferenceError: XMLHttpRequest is not defined
      at file:///var/task/server/app.mjs:2522:21
      at new Promise (<anonymous>)
      at fetch2 (file:///var/task/server/app.mjs:2517:16)
      at file:///var/task/server/app.mjs:2646:7
      at new Promise (<anonymous>)
      at file:///var/task/server/app.mjs:2645:12
      at Generator.next (<anonymous>)
      at file:///var/task/server/app.mjs:2619:67
      at new Promise (<anonymous>)
      at __awaiter$8 (file:///var/task/server/app.mjs:2601:10)
}

To Reproduce

I made a repository exposing the issue : https://github.com/Pixselve/supabase-sveltekit-endpoints Click on a button to fetch the endpoint and observe the cloud functions logs on Vercel.

Expected behavior

Requests should not fail and should give the appropriate data.

System information

  • OS: Vercel Node.js Runtime
  • Version of supabase-js: 1.10.0
  • Version of Node.js: 14

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:27 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
sbutler-ghcommented, Nov 23, 2021

@sbutler-gh I think your repo isn’t actually using the up-to-date version of @supabase/supabase-js, see here:

sbutler-gh/just-start@c32ecef/package-lock.json#L325-L326

It appears it’s using version 1.24.0, and the ability to pass in a custom fetch implementation was added in 1.27.0 (and 1.28.1 is the latest version).

After running npm update to update packages and re-deploying, it now works as expected in production on Cloudflare Pages! (using fetch: (...args) => fetch(...args), didn’t try the other syntaxes yet.). Thank you SO MUCH @jacobwgillespie ! I can’t thank you enough!

5reactions
kiwicopplecommented, Nov 8, 2021

Thanks to a very impressive series of PRs from @jacobwgillespie , this now has a workaround

https://github.com/supabase/supabase-js/releases/tag/v1.27.0

after upgrading to v1.27.0 you can use a custom fetch implementation

import { createClient } from '@supabase/supabase-js'

// Provide a custom `fetch` implementation as an option
const supabase = createClient('https://xyzcompany.supabase.co', 'public-anon-key', { fetch: fetch })

Jacob - if you send your details (and tshirt size) to swag@supabase.io we will send you a nice package 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

XMLHttpRequest is not defined in frontend code
I am using svelte, and all the questions about this error are about backend node.js code. javascript · html · typescript · frontend...
Read more >
Endpoints – Vercel Docs
In this module, we will learn how to create server-side routes using SvelteKit endpoints.
Read more >
Make a Simple API Endpoint with Vercel Serverless Functions
Deploy to Vercel. Righty ho! Time to deploy to Vercel! The script is already in the project when I defined the start and...
Read more >
Deploying SvelteKit App to Vercel : r/sveltejs - Reddit
Test run the setup by running npm run build , this should create a .vercel_build_output folder and produce no errors. Push your app...
Read more >
Profile for Netlify - Linknovate
Let's look at what we've learned: How to set up SvelteKit and create ... Benefits: Quick deployment Secure (no dynamic content) Fast load...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found