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.

issue with fetch while using adapter-cloudflare-workers

See original GitHub issue

Describe the bug I am using supabase-js with sveltekit and cloudflare-workers. While using adapter-cloudflare-workers, I get hit with following issue when I do wrangler dev.

Uncaught
ReferenceError: require is not defined
    at node_modules/node-fetch/lib/index.js (worker.js:59:18)
    at __require (worker.js:9:44)
    at node_modules/cross-fetch/dist/node-ponyfill.js (worker.js:1099:21)
    at __require (worker.js:9:44)
    at node_modules/@supabase/gotrue-js/dist/main/lib/fetch.js (worker.js:1152:41)
    at __require (worker.js:9:44)
    at node_modules/@supabase/gotrue-js/dist/main/GoTrueApi.js (worker.js:1442:19)
    at __require (worker.js:9:44)
    at node_modules/@supabase/gotrue-js/dist/main/index.js (worker.js:2149:39)
    at __require (worker.js:9:44)

This issue happens because supabase-js is using cross-fetch as its fetch-polyfill. I found a workaround by applying following patches

  • changing platform from node to browser in the esbuild options inside adapter-cloudflare-worker.
  • patching cross-fetch to export native fetch for browser polyfill.

node may have been chosen as a platform of choice for esbuild, since cloudflare has started supporting node libraries for its workers platform.

However, cloudflare provides a hybrid of browser(service-worker) and node environment with native fetch support. Hence, if I keep node as a platform, then I face issues with browser’s native object such as fetch, websocket that are also available under cloudflare’s service-worker environment.

I have couple of questions here

  • Would it make sense to change the platform from node to browser. This will essentially conform to intrinsic nature of service-worker environment supporting many of the browser’s native objects.
  • Is there a way to customise esbuild options used for a given adapter?
  • As per esbuild team, to override any external dependency, one way is to write a custom plugin such as esbuild-plugin-external-global. Is there a way to pass custom plugin or 3rd party plugin to esbuild config used during adapter build?

Logs

I have provided the error logs in my issue above

To Reproduce Here’s a sample repo.

supabase url + apikey and cloudflare account_id + zone_id will be required to reproduce issue

Expected behavior

cloudflare-worker adapter should allow options to customise esbuild so that third party libraries using cross-fetch can be integrated to cloudflare environment.

Stacktraces If you have a stack trace to include, we recommend putting inside a <details> block for the sake of the thread’s readability:

Stack trace
Uncaught
ReferenceError: require is not defined
    at node_modules/node-fetch/lib/index.js (worker.js:59:18)
    at __require (worker.js:9:44)
    at node_modules/cross-fetch/dist/node-ponyfill.js (worker.js:1099:21)
    at __require (worker.js:9:44)
    at node_modules/@supabase/gotrue-js/dist/main/lib/fetch.js (worker.js:1152:41)
    at __require (worker.js:9:44)
    at node_modules/@supabase/gotrue-js/dist/main/GoTrueApi.js (worker.js:1442:19)
    at __require (worker.js:9:44)
    at node_modules/@supabase/gotrue-js/dist/main/index.js (worker.js:2149:39)
    at __require (worker.js:9:44)

Information about your SvelteKit Installation:

Diagnostics
System:
    OS: macOS 11.4
    CPU: (4) x64 Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
    Memory: 21.73 MB / 16.00 GB
    Shell: 3.2.1 - /usr/local/bin/fish
  Binaries:
    Node: 16.3.0 - ~/.local/share/nvm/v16.3.0/bin/node
    npm: 7.15.1 - ~/.local/share/nvm/v16.3.0/bin/npm
  Browsers:
    Chrome: 91.0.4472.101
    Chrome Canary: 93.0.4539.0
    Edge: 91.0.864.41
    Firefox: 86.0.1
    Safari: 14.1.1
  npmPackages:
    @sveltejs/kit: next => 1.0.0-next.114
    svelte: ^3.34.0 => 3.38.2

Severity This is hampering me from running supabase together with sveltekit on cloudflare. I believe that this issues goes beyond supabase. If not supabase, many other libraries use cross-fetch to access their apis. Hence, they may potentially face this issue.

Additional context

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
Rich-Harriscommented, May 12, 2022

Closing since there’s a viable workaround. Hopefully libraries will stop bundling things like cross-fetch, now that more and more runtimes expose it natively

2reactions
The-Noahcommented, Jan 19, 2022

This fixed it for me:

{
  fetch: (...args) => fetch(...args),
}

Source: https://github.com/supabase/postgrest-js/pull/235

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plaid Package Issue - Workers - Cloudflare Community
The first step is, the client makes a fetch call to the worker, ... [ERROR] Uncaught (in response) TypeError: adapter is not a...
Read more >
TypeError: (e.adapter || s.adapter) is not a function - Workers
I ran into this problem when I tried to use the Square API on Cloudflare Workers, so I made an Axios native fetch...
Read more >
Are there any restrictions on Cloudflare workers to make fetch ...
Just as described in the title, I experienced a 5xx error when I just proxy Cloudflare worker A behind worker B. I use...
Read more >
@hattip/adapter-cloudflare-workers - npm
Start using @hattip/adapter-cloudflare-workers in your project by running ... export default { fetch: cloudflareWorkersAdapter(handler), };.
Read more >
Adapters • Docs • SvelteKit
By default, projects are configured to use @sveltejs/adapter-auto , which detects ... For example, Cloudflare Workers can access an env object containing KV ......
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