Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vite 4 to Vite 5 causes @supabase/node-fetch to use server module in browser - leads to global is not defined error #15274

Closed
7 tasks done
jacob-8 opened this issue Dec 7, 2023 · 6 comments

Comments

@jacob-8
Copy link

jacob-8 commented Dec 7, 2023

Describe the bug

I'm updating a SvelteKit project from Vite 4 to 5 and Supabase throws an error once the client hydrates global is not defined. I'm not sure if this is a Vite bug, a SvelteKit bug, or a Supabase bug. But it's a bug relating to how Vite chooses which server or browser file to serve up. For reference, in Vite 4, @supabse/node-fetch loads a browser version (browser.js) client-side:

image

But in Vite 5 it loads the server version (index.js) on the client thus causing the problem:
image

I've also posted this in Supabase: supabase/postgrest-js#465 - I hope it's just a simple issue of Supabase needing to update a few lines in how they define imports in a package.json.

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-8hs1lt?file=package.json

Steps to reproduce

Works with Vite 4: https://stackblitz.com/edit/sveltejs-kit-template-default-tnrxgs?file=package.json
Doesn't work with Vite 5: https://stackblitz.com/edit/sveltejs-kit-template-default-8hs1lt?file=package.json

  • Go to sveltekit.new
  • npm i -D @supabase/supabase-js
  • npm run dev
  • import anything from @supabase/supabase-js and get error when the client hydrates.

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.10.5 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^5.0.6 => 5.0.6

Used Package Manager

npm

Logs

No response

Validations

@mkilpatrick
Copy link

I posted this link in the supabase issue, but it's most likely how they're incorrectly exporting in their package.json.

https://publint.dev/@supabase/postgrest-js@1.9.0

@bluwy
Copy link
Member

bluwy commented Dec 7, 2023

Seems like this is a bug that you'd need the latest version (v3) of @sveltejs/vite-plugin-svelte to fix this, which SvelteKit doesn't use yet. The plugins v2 version only supports Vite 4. sveltejs/vite-plugin-svelte#808

I'd suggest waiting for SvelteKit to support Vite 5 first, or you can try overriding @sveltejs/vite-plugin-svelte to use the latest version. Or this workaround with this config for now:

// vite.config.js

	resolve: {
		mainFields: ['browser', 'module', 'jsnext:main', 'jsnext']
	}

@bluwy bluwy closed this as completed Dec 7, 2023
@jacob-8
Copy link
Author

jacob-8 commented Dec 9, 2023

I'd suggest waiting for SvelteKit to support Vite 5 first

@bluwy I sort of thought I might be jumping the gun. Sure, I'll just wait for SvelteKit to come along with support for Vite 5. Thanks for the quick answer!

@jacob-8
Copy link
Author

jacob-8 commented Dec 9, 2023

I posted this link in the supabase issue, but it's most likely how they're incorrectly exporting in their package.json.

Thank you for doing that! I had not known of that tool before and hopefully that will also help improve things.

@jacob-8
Copy link
Author

jacob-8 commented Dec 15, 2023

I can confirm that there is no bug when using Vite 5 with SvelteKit 2 and @sveltejs/vite-plugin-svelte v3 - https://stackblitz.com/edit/sveltejs-kit-template-default-x9ybq2?file=package.json

@github-actions github-actions bot locked and limited conversation to collaborators Dec 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants