supabase: Supabase fails to load in web component

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

Adding a component that uses Supabase to Shoelace components produces the following error:

SyntaxError: The requested module './../../../node-fetch/lib/index.js' does not provide an export named 'default' (at PostgrestBuilder.js:1:8)

Which references this line in PostgrestBuild.js:

// @ts-ignore
import nodeFetch from '@supabase/node-fetch';

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. git clone https://github.com/shoelace-style/shoelace
  2. cd shoelace
  3. npm install @supabase/supabase-js
  4. npm install
  5. npm run build
  6. npm run test => All tests pass, check.
  7. Add Supabase to one of the components such as src/components/alert/alert.component.ts by adding the following two lines after the existing import statements:
import { createClient } from '@supabase/supabase-js'
createClient('https://xyzcompany.supabase.co', 'public-anon-key')
  1. npm run test
  2. Observe that now all tests fail to even load, each giving an error of the following form:
src/components/alert/alert.test.ts:

 đźš§ Browser logs on Chromium:
      SyntaxError: The requested module './../../../node-fetch/lib/index.js' does not provide an export named 'default'
 đźš§ Browser logs on Webkit:
      SyntaxError: Importing binding name 'default' cannot be resolved by star export entries.

 ❌ Could not import your test module. Check the browser logs or open the browser in debug mode for more information. 

Expected behavior

Adding Supabase to a component should not cause its test and the tests of all other components in the package to fail to load.

System information

  • OS: macOS Sonoma 14.2.1
  • Browsers tested: chrome, safari
  • Version of supabase-js: 2.39.6
  • Version of Node.js: 21.5.0

Related report

This open issue appears to be related, but since it did not include a detailed means to reproduce, I thought I’d create a new issue to facilitate reproduction in hopes to resolve this more quickly:

(I initially opened an issue at @supabase/postgrest-js repo, but this problem is more pervasive as it prevents any Supabase functions from working since supabase-js cannot even load.)

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (6 by maintainers)

Most upvoted comments

@encima: Thank you for re-opening.

I see you’ve removed bug and added needs-analysis. Ok, let me try to jumpstart analysis:

  • For the browser target, node-fetch should not even be needed because browsers have native fetch support.
  • Every @supabase/* package has the following package.json properties:
  "main": "dist/main/index.js",
  "module": "dist/module/index.js",
  • …except @supabase/node-fetch, which is noted in the error message as failing to import in PostgrestBuilder.js:1:8.

The above suggests that Supabase is perhaps not robustly handling its fetch() needs for ESM modules in the browser, either because it ought to be using the browser-native fetch() rather than node-fetch or because @supabase/node-fetch is not configured in the manner of all other @supabase/* packages wrt the above noted package.json properties.

This improper importing of supabase node-fetch inside supabase-postgrest is causing vitest not to run at all. We have confirmed that removing the supabase package fixes this issue. We have dozens of dependencies across multiple projects and only this one causes this issue.

 FAIL  test/entry-date.spec.ts [ test/entry-date.spec.ts ]
Error: No such module "Users/bob/notes/node_modules/.pnpm/@supabase+postgrest-js@1.16.3/node_modules/@supabase/postgrest-js/dist/cjs/@supabase/node-fetch".
 ❯ Users/bob/notes/node_modules/.pnpm/@supabase+postgrest-js@1.16.3/node_modules/@supabase/postgrest-js/dist/cjs/PostgrestBuilder.js?mf_vitest_no_cjs_esm_shim:7:38
 ❯ Users/bob/notes/node_modules/.pnpm/@supabase+postgrest-js@1.16.3/node_modules/@supabase/postgrest-js/dist/cjs/PostgrestTransformBuilder.js?mf_vitest_no_cjs_esm_shim:6:44
 ❯ Users/bob/notes/node_modules/.pnpm/@supabase+postgrest-js@1.16.3/node_modules/@supabase/postgrest-js/dist/cjs/PostgrestFilterBuilder.js?mf_vitest_no_cjs_esm_shim:6:53
 ❯ Users/bob/notes/node_modules/.pnpm/@supabase+postgrest-js@1.16.3/node_modules/@supabase/postgrest-js/dist/cjs/PostgrestQueryBuilder.js?mf_vitest_no_cjs_esm_shim:6:50
 ❯ Users/bob/notes/node_modules/.pnpm/@supabase+postgrest-js@1.16.3/node_modules/@supabase/postgrest-js/dist/cjs/PostgrestClient.js?mf_vitest_no_cjs_esm_shim:6:49
 ❯ Users/bob/notes/node_modules/.pnpm/@supabase+postgrest-js@1.16.3/node_modules/@supabase/postgrest-js/dist/cjs/index.js?mf_vitest_no_cjs_esm_shim:8:43

@kjhughes I’m actually here to try and fix the issue. I’ll investigate a bit more and try to create reproduction steps and then a fix for it. Also note the Supabase team always tries to fix higher priority stuff first, I’m no longer a Supabase staff but I know how things work internally and know stuff like this isn’t ignored. They are just not as high of a priority as other tasks that user’s are requesting for, a case and point is I have never ran into this issue before and I have working playwright tests in my project and this hasn’t shown up.

@encima : If your argument is that my issue title is too broad in saying “web components”, you’ve proven your point. I am fine with narrowing the scope of the description per your suggestion.

However, the problem remains that I cannot call createClient() because import { createClient } from '@supabase/supabase-js' is failing in a context that uses well-established techniques. I’ve provided the exact steps to reproduce, and the only change to the working tests is the addition of

import { createClient } from '@supabase/supabase-js'
createClient('https://xyzcompany.supabase.co', 'public-anon-key')

I should be able to call createClient() in this manner. At least two others that I’ve found have reported receiving a similar error in another context. I thought it would help to provide specific, easily reproducible steps that might be used to identify the problem.