Is there a way to use `pg` in a Next.js project setup with `@cloudflare/next-on-pages?
I have project that is setup following https://developers.cloudflare.com/pages/framework-guides/deploy-a-nextjs-site/, and now I tried to use
pg
to connect to a remote database. As soon as I import and use pg
in one of my funtions though, I get errors like this during npm run build
:
How can I use pg
in a Next.js project that I want to deploy to Cloudflare Pages?9 Replies
Quick demo repository: https://github.com/janpio/cloudflare-next-prisma/tree/plain-pg
+1 as this is a blocker stopping me from migrating away from accelerate w prisma 🙂
Now also an issue at https://github.com/cloudflare/next-on-pages/issues/605
GitHub
[🐛 Bug]:
pg
does not work · Issue #605 · cloudflare/next-on-pagesnext-on-pages environment related information System: Platform: linux Arch: x64 Version: #202312122204 SMP PREEMPT_DYNAMIC Tue Dec 12 22:43:56 UTC 2023 CPU: (16) x64 AMD EPYC 7B13 Memory: 63 GB She...
pg requires node_compat which Pages doesn't support
I believe you can use Postgres.js with nodejs_compat though (the flag)
node_compat is a special workers only build option which uses esbuild to bundle in some node polyfills
nodejs_compat is a flag you can enable and enables some polyfills within the worker runtime already on the node: prefix
they were working on adding node-compat polyfill to Pages: https://github.com/cloudflare/workers-sdk/pull/2541
actually you commented on that so I don't get your confusion then, pg is just looking for stuff only node_compat can polyfill like steam/path
This doesn't even get to use any compat modes, as
pg
does not get the Cloudflare compatible version during the Next.js build that has to happen for @cloudflare/next-on-pages
. If that worked, I am pretty confident we could deploy it to Cloudflare Pages via that pre-release version of the CLI.
So my question really is specific to @cloudflare/next-on-pages
, which seems to be missing a way to get pg
use the right code during the Next.js build phase.Well, what's actually failing is
next build
isn't it? so isn't this an issue with next itself not correctly resolving the import?
basically shouldn't this issue be something like pg doesn't work in routes using the edge runtime
and opened in the Next.js gh repository?
As long as that doesn't work in Next.js itself I don't think it is surprising that it doesn't work with @cloudflare/next-on-pages
besides the above there's still the node_compat
flag issue, even if the flag could work with Pages, keep in mind that @cloudflare/next-on-pages
requires the nodejs_compat
compatibility flag set and node_compat
and nodejs_compat
cannot be both used at the same time, so I think that we can't assume that node_compat
can actually be used here in any case
(maybe @cloudflare/next-on-pages
could itself add some partial nodejs polyfills that are still compatible with nodejs_compat
... although I fear that that could lead into a bit of a slippery slope)I honestly don't really know if the problem is with Next.js or with
next-on-pages
.
I can't run Next.js on Cloudflare Pages without next-on-pages
, so I can not really confirm what is going on and opened the issue.
In the issue I expressed this theory:
I theorize that the problem is because pg
was made to use a adapter version of some code when running in workerd. But during the Next.js build this is not set, so it thinks it is running in a normal Node environment and includes the wrong code - which then would not work in edge-light which the Next.js bundler assumes for edge functions - and hence fails.
To work on Cloudflare Workers (or Pages Functions) pg
needs to use a different import so it can use the Cloudflare Socket API, and not use Node APIs that are not available.
That works fine if you use pg
in a normal worker via wrangler
, but my theory was that with next-on-pages
this somehow does not happen during the build (as that runs locally via next
CLI), and hence then the built functions are wrong.
But even here I have no idea if this really make sense as I do not understand how next-on-pages
really works under the hood.
In the end I see that I can not use next-on-pages
and pg
.
basically shouldn't this issue be something like pg doesn't work in routes using the edge runtime and opened in the Next.js gh repository?Cloudflare Workers is unfortunately the only Edge runtime that offers a TCP API that
pg
can use.
It is expected that pg
does not work anywhere else.Hey y'all are there any updates on this? As I'm building out another project, want to host it on the edge, but am concerned that I won't be able to without using accelerate? My new stack would be turso/nextjs etc.
@janpio (Prisma) I see the documentation for prisma x edge working on planetscale, not working on pg/neon but I don't see anything about working or not working on sqlite dbs?
The
pg
situation will hopefully be resolved soon, but not fully done yet.
For SQLite you can use the Turso driver, which also supports a local file mode afaik: https://www.prisma.io/docs/orm/overview/databases/turso (We have no examples for that local file mode yet)