supabase-js icon indicating copy to clipboard operation
supabase-js copied to clipboard

2.49.9 breaks application code on Vercel (error 500)

Open wiesson opened this issue 1 year ago • 66 comments

Bug report

Describe the bug

    at (../../../../../node_modules/.pnpm/[email protected]/node_modules/ws/browser.js:4:8)
    at (../../../../../node_modules/.pnpm/@[email protected]/node_modules/@supabase/realtime-js/src/RealtimeClient.ts:222:20)
    at (../../../../../node_modules/.pnpm/@[email protected]/node_modules/@supabase/realtime-js/src/RealtimeChannel.ts:218:18)

and the app errors with status 500

To Reproduce

Adding later

Expected behavior

I have updated from 2.49.8

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • Version of supabase-js: [e.g. 2.49.9]
  • Version of Node.js: [e.g. 22.X] (vercel)

wiesson avatar Jun 03 '25 05:06 wiesson

Hi @wiesson thank you for reporting the issue, we're working on fix for it, would be great if you could try out the version "@supabase/supabase-js": "2.49.9-next.3" and confirm if that fixed it for you.

Thanks.

grdsdev avatar Jun 03 '25 10:06 grdsdev

I just deployed a version with "@supabase/supabase-js": "2.49.9-next.3", but I have the same error.

Error: ws does not work in the browser. Browser clients must use the native WebSocket object
    at (../../../../../node_modules/.pnpm/[email protected]/node_modules/ws/browser.js:4:8)
    at (../../../../../node_modules/.pnpm/@[email protected]/node_modules/@supabase/realtime-js/src/RealtimeClient.ts:222:20)
    at (../../../../../node_modules/.pnpm/@[email protected]/node_modules/@supabase/realtime-js/src/RealtimeChannel.ts:218:18)

It works with 2.49.8, so I'm not sure if I'm supposed to change sth

wiesson avatar Jun 03 '25 13:06 wiesson

@wiesson can you check with the latest version of supabase-js?

grdsdev avatar Jun 04 '25 14:06 grdsdev

Hey! Seeing this error now:

../../node_modules/.pnpm/@[email protected]/node_modules/@supabase/realtime-js/dist/main/RealtimeClient.js
Critical dependency: the request of a dependency is an expression

thisislvca avatar Jun 04 '25 15:06 thisislvca

are you able to provide some example code so we can replica the issue?

also reopening this issue

filipecabaco avatar Jun 04 '25 15:06 filipecabaco

Will try to create a repro when I get back home later. Here are some additional logs & details in the meantime:

../../node_modules/.pnpm/@[email protected]/node_modules/@supabase/realtime-js/dist/main/RealtimeClient.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
../../node_modules/.pnpm/@[email protected]/node_modules/@supabase/realtime-js/dist/main/RealtimeClient.js
../../node_modules/.pnpm/@[email protected]/node_modules/@supabase/realtime-js/dist/main/index.js
../../node_modules/.pnpm/@[email protected]/node_modules/@supabase/supabase-js/dist/module/index.js
../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@supabase/ssr/dist/module/createBrowserClient.js
../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@supabase/ssr/dist/module/index.js
./src/lib/supabase/server.ts

Actually now that I look at the import trace it looks like the ssr module is using createBrowserClient under the hood, even for the server client. Here's my supabase/server.ts (copied straight from the docs).

import 'server-only';

import { createServerClient } from '@supabase/ssr';
import type { Database } from 'common/types/database';
import { cookies } from 'next/headers';

export function createClient() {
  const cookieStore = cookies();

  return createServerClient<Database>(
    process.env.NEXT_PUBLIC_SUPABASE_URL!,
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
    {
      cookies: {
        getAll() {
          return cookieStore.getAll();
        },
        setAll(cookiesToSet) {
          try {
            cookiesToSet.forEach(({ name, value, options }) => cookieStore.set(name, value, options));
          } catch {
            // The `setAll` method was called from a Server Component.
            // This can be ignored if you have middleware refreshing
            // user sessions.
          }
        },
      },
    },
  );
}

thisislvca avatar Jun 04 '25 15:06 thisislvca

OK so the repro is literally the official nextjs template: npx create-next-app -e with-supabase.

#1442 might also be related.

thisislvca avatar Jun 04 '25 20:06 thisislvca

Error: ws does not work in the browser. Browser clients must use the native WebSocket object
    at (../../../../../node_modules/.pnpm/[email protected]/node_modules/ws/browser.js:4:8)
    at (../../../../../node_modules/.pnpm/@[email protected]/node_modules/@supabase/realtime-js/src/RealtimeClient.ts:222:20)
    at (../../../../../node_modules/.pnpm/@[email protected]/node_modules/@supabase/realtime-js/src/RealtimeChannel.ts:218:18)

Still the same error, is 2.11.10 the correct version to test with?

wiesson avatar Jun 04 '25 20:06 wiesson

Yeah, it's the latest version. Honestly, I kinda expected this issue to get more attention, considering it breaks builds...

Edit: it actually doesn't break builds, it compiles with warnings

thisislvca avatar Jun 04 '25 20:06 thisislvca

Edit: it actually doesn't break builds, it compiles with warnings

So true! I've updated the title. I quickly created this so I wouldn't forget to do it

wiesson avatar Jun 04 '25 21:06 wiesson

@filipecabaco just wanted to ping you in case you didn't see the repro already

thisislvca avatar Jun 05 '25 21:06 thisislvca

  "scripts": {
    "dev": "next dev --turbopack",
    "dev:webpack": "next dev",
    "build": "next build",
    "start": "next start",
    ... other commands

  },

   "dependencies":{
     ...other libs
    "@supabase/supabase-js": "^2.49.10",
}

With Webpack

$ npm run dev:webpack

> [email protected] dev:webpack
> next dev

✔ Console Ninja extension is connected to Next.js, see https://tinyurl.com/2vt8jxzw
   ▲ Next.js 15.3.3
   - Local:        http://localhost:3000
   - Network:      http://192.168.3.7:3000
   - Environments: .env.local, .env

 ✓ Starting...
 ✓ Ready in 3.7s
 ✓ Compiled /middleware in 301ms (119 modules)
 ○ Compiling /page/invoices ...
 ⚠ ./node_modules/@supabase/realtime-js/dist/main/RealtimeClient.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/@supabase/realtime-js/dist/main/RealtimeClient.js
./node_modules/@supabase/realtime-js/dist/main/index.js
./node_modules/@supabase/supabase-js/dist/module/index.js
./node_modules/@supabase/ssr/dist/module/createBrowserClient.js
./node_modules/@supabase/ssr/dist/module/index.js
./src/lib/supabase/server.ts

with Turbopack

$ npm run dev

> [email protected] dev
> next dev --turbopack

✔ Console Ninja extension is connected to Next.js, see https://tinyurl.com/2vt8jxzw
   ▲ Next.js 15.3.3 (Turbopack)
   - Local:        http://localhost:3000
   - Network:      http://192.168.3.7:3000
   - Environments: .env.local, .env

 ✓ Starting...
 ✓ Compiled middleware in 263ms
 ✓ Ready in 3.3s
 ○ Compiling /page/invoices ...
 ✓ Compiled /page/invoices in 8.3s
 GET /page/invoices 200 in 9892ms
 ○ Compiling /_not-found/page ...
 ✓ Compiled /_not-found/page in 1771ms
 GET /.well-known/appspecific/com.chrome.devtools.json 404 in 2375ms
 ○ Compiling /api/auth/get-session-token ...

When I run with webpack with the newest version as of today June 5, 2025, I get that error, but when I run with turbopack I don't get that error.

Then I looked in the code and noticed that it made a curious dynamic import:

            _fetch = (...args) => Promise.resolve(`${'@supabase/node-fetch'}`).then(s => __importStar(require(s))).then(({ default: fetch }) => fetch(...args));

nasatome avatar Jun 06 '25 03:06 nasatome

thank you! that's an extremely useful discovery! we might need to do the same fix we did for WS but now one for this fetch lib 😓

filipecabaco avatar Jun 06 '25 08:06 filipecabaco

next.config.ts

import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'lh3.googleusercontent.com',
      },
    ],
  },
  webpack: (config) => {
    // Configure webpack to prefer ESM builds for browser environments
    config.resolve.mainFields = ['browser', 'module', 'main'];
    
    // Force webpack to use browser conditions for exports field resolution
    config.resolve.conditionNames = ['browser', 'module', 'import', 'default'];
    
    return config;
  },
};

export default nextConfig;

today: version 2.49.10 works with this nextconfig

i made a PR: https://github.com/supabase/realtime-js/pull/481

I ask the community for help in analyzing the fetch solution I proposed.

if you consider that new function has strong security implications in spite of being with hardcoded strings, you can ignore it, but if you consider it viable, let me know to upload a new PR

resume:

 /**
   * Use either custom fetch, if provided, or default fetch to make HTTP requests
   *
   * @internal
   */
  _resolveFetch = (customFetch?: Fetch): Fetch => {
    let _fetch: Fetch
    if (customFetch) {
      _fetch = customFetch
    } else if (typeof fetch === 'undefined') {
      _fetch = (...args) => {
        // Try dynamic import with different strategies based on environment
        return this._attemptNodeFetch(args)
      }
    } else {
      _fetch = fetch
    }
    return (...args) => _fetch(...args)
  }

  /**
   * Attempts to load node-fetch with environment-specific strategies
   * @internal
   */
  private async _attemptNodeFetch(args: Parameters<Fetch>): Promise<Response> {
    // Strategy 1: Try new Function() approach (works in most environments)
    try {
      if (this._canUseFunction()) {
        const dynamicImport = new Function(
          'specifier',
          'return import(specifier)'
        )
        const module = await dynamicImport('@supabase/node-fetch')
        return module.default(...args)
      }
    } catch (error) {
      // Continue to next strategy
    }

    // Strategy 2: Direct import (may cause webpack warnings but works in Edge environments)
    try {
      // @ts-ignore - Dynamic import fallback for Edge environments
      const module = await import('@supabase/node-fetch')
      return (module.default as Fetch)(...args)
    } catch (importError) {
      // Continue to next strategy
    }

    // Strategy 3: Fallback to require for legacy Node.js environments
    try {
      if (this._canUseFunction()) {
        const dynamicRequire = new Function(
          'specifier',
          'return require(specifier)'
        )
        const nodeFetch = dynamicRequire('@supabase/node-fetch')
        return Promise.resolve((nodeFetch.default || nodeFetch)(...args))
      } else {
        // Direct require fallback (works in some Node.js environments)
        const nodeFetch = require('@supabase/node-fetch')
        return Promise.resolve((nodeFetch.default || nodeFetch)(...args))
      }
    } catch (requireError) {
      // All strategies failed
      throw new Error(
        'fetch polyfill required: install @supabase/node-fetch or use Node.js 18+. ' +
          'Current environment does not support dynamic imports or require.'
      )
    }
  }

  /**
   * Checks if new Function() is available (CSP restrictions)
   * @internal
   */
  private _canUseFunction(): boolean {
    try {
      new Function('return true')()
      return true
    } catch {
      return false
    }
  }

nasatome avatar Jun 06 '25 08:06 nasatome

I'm having this same issue with anything above 2.49.8 using Solid-Start.

Here are my logs when upgrading to either 49.9 or 50:

node_modules/.pnpm/@[email protected]/node_modules/@supabase/supabase-js/dist/module/SupabaseClient.js (1:16): Error when using sourcemap for reporting an error: Can't resolve original location of error.
node_modules/.pnpm/@[email protected]/node_modules/@supabase/supabase-js/dist/module/SupabaseClient.js (1:24): Error when using sourcemap for reporting an error: Can't resolve original location of error.
node_modules/.pnpm/@[email protected]/node_modules/@supabase/auth-js/dist/module/GoTrueAdminApi.js (1:13): Error when using sourcemap for reporting an error: Can't resolve original location of error.
node_modules/.pnpm/@[email protected]/node_modules/@supabase/auth-js/dist/module/GoTrueAdminApi.js (1:21): Error when using sourcemap for reporting an error: Can't resolve original location of error.
node_modules/.pnpm/@[email protected]/node_modules/@supabase/functions-js/dist/module/FunctionsClient.js (1:16): Error when using sourcemap for reporting an error: Can't resolve original location of error.
node_modules/.pnpm/@[email protected]/node_modules/@supabase/functions-js/dist/module/FunctionsClient.js (1:24): Error when using sourcemap for reporting an error: Can't resolve original location of error.
node_modules/.pnpm/@[email protected]/node_modules/@supabase/supabase-js/dist/module/lib/fetch.js (1:16): Error when using sourcemap for reporting an error: Can't resolve original location of error.
node_modules/.pnpm/@[email protected]/node_modules/@supabase/supabase-js/dist/module/lib/fetch.js (1:24): Error when using sourcemap for reporting an error: Can't resolve original location of error.

2.49.8 works fine though

caseybaggz avatar Jun 09 '25 13:06 caseybaggz

I also have the Critical dependency: the request of a dependency is an expression warning since very recently.

harrybawsac avatar Jun 11 '25 09:06 harrybawsac

Adding on that we're seeing Critical dependency: the request of a dependency is an expression with 2.50.0, and that it is causing some realtime subscriptions to fail to receive any payloads.

Revert to 2.49.8 has resolved the problem

wmurphyrd avatar Jun 12 '25 14:06 wmurphyrd

I am also getting this same issue in 2.50.0. I have not seen any side-effects yet and it only appears as a warning.

muezz avatar Jun 12 '25 16:06 muezz

I am also getting these warnings after using 2.50.0. Not seeing any side effects, but it's annoying to see so many warnings each time it gets called. Please let us know once this is fixed.

ckhasnis avatar Jun 13 '25 00:06 ckhasnis

thank you! that's an extremely useful discovery! we might need to do the same fix we did for WS but now one for this fetch lib 😓

Sounds good! Let us know if there's anything we need to do/can do on our side

thisislvca avatar Jun 13 '25 06:06 thisislvca