issue with fetch while using adapter-cloudflare-workers
See original GitHub issueDescribe 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
tobrowser
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
tobrowser
. 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:
- Created 2 years ago
- Reactions:1
- Comments:8 (6 by maintainers)
Closing since there’s a viable workaround. Hopefully libraries will stop bundling things like
cross-fetch
, now that more and more runtimes expose it nativelyThis fixed it for me:
Source: https://github.com/supabase/postgrest-js/pull/235