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/pg-cloudflare@1.1.1/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)
Was this page helpful?