Support Node Compat in Cloudflare Adaptor
arlair opened this issue · 1 comments
Describe the problem
Cloudflare announced Connect to PostgreSQL and it can provide database access from Cloudflare Workers, which combined with SvelteKit would be very powerful:
I'm not sure if it is meant to work as the documentation is for Cloudflare Workers and some old links suggest node_compat does not work in Pages. However, the documentation suggests otherwise, as I am using Cloudflare Pages with node_compat flags enabled via the Cloudflare Dashboard as found here:
I tried with a project and found a local vite build would fail due to pg
being a node library when using the cloudflare-adaptor.
Describe the proposed solution
I found this build-worker
project, that includes the @esbuild-plugins/node-modules-polyfill
with how to use it here:
and it turns out Wrangler also uses this library:
I made my own copy of @sveltejs/adapter-cloudflare
to test and added this library. It builds locally and in Cloudflare Pages. While my code compiles and deploys, it does not seem to work as logging from Cloudflare Pages Functions gives the error: ReferenceError: process is not defined
error, so I am not sure if I am missing something for the correct polyfill, or it is not possible at the moment due to a Cloudflare Pages limitation.
The documentation states Node.js APIs are available under the node: prefix, and this prefix must be used when importing modules, both in your code and the npm packages you depend on
. However, the database connect documentation does not mention this when it tells you to add the pg
package and pg
would not be using the node:
prefix internally. I also thought I found mention of another proposed option where the node:
prefix was not required, but unfortunately I cannot find it.
Alternatives considered
Wrangler 2 source code is a lot more complicated and seems to have a nodejsCompat
option that can be passed that is used here:
This announcement mentions the Node.js APIs are provided directly by the
open-source Cloudflare Workers runtime, with no need to bundle polyfill implementations into your own code.
, so perhaps we only need to be able to compile it without the polyfill and then the nodejs_compat
flag handles it?
Importance
would make my life easier
Additional Information
No response