Use `pg` in Pages Function?

I want to access a PostgreSQL database in a function that will be deployed via Cloudflare Pages. Workers supports using pg, the common Node PostgreSQL database. But I can not make it work with the Functions in Pages. I get errors like this on pnpm wrangler pages dev . -:
✘ [ERROR] Could not resolve "events"

../node_modules/.pnpm/[email protected]/node_modules/pg-cloudflare/dist/index.js:1:29:
1 │ import { EventEmitter } from 'events';
╵ ~~~~~~~~

The package "events" wasn't found on the file system but is built into node.
Add the "nodejs_compat" compatibility flag to your Pages project to enable Node.js compatibility.
✘ [ERROR] Could not resolve "events"

../node_modules/.pnpm/[email protected]/node_modules/pg-cloudflare/dist/index.js:1:29:
1 │ import { EventEmitter } from 'events';
╵ ~~~~~~~~

The package "events" wasn't found on the file system but is built into node.
Add the "nodejs_compat" compatibility flag to your Pages project to enable Node.js compatibility.
If I understand the error message correctly, I am supposed to do pnpm wrangler pages dev . --compatibility-flag="nodejs_compat" - but that returns exactly the same error. (I know that in Workers I actually need to use node_compat (which sounds similar, but it something very different) - but that does not seem to exist for Pages)
2 Replies
kian
kian12mo ago
pg does indeed require node_compat, not nodejs_compat. You could use Postgres.js in Pages with nodejs_compat.
Jan Piotrowski (janpio)
Unfortunately not really an option as I am testing a library (Prisma) here that enables usage of pg, because that is what the library users are asking for. I found https://github.com/cloudflare/workers-sdk/issues/1074 and https://github.com/cloudflare/workers-sdk/pull/2541, but seems there was no conclusion to these.
Want results from more Discord servers?
Add your server