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:

$ npm run build

> next-pg@0.1.0 build
> next build

   ▲ Next.js 14.0.4

Failed to compile.

./node_modules/pg-connection-string/index.js:76:69
Module not found: Can't resolve 'fs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/pg/lib/connection-parameters.js
./node_modules/pg/lib/client.js
./node_modules/pg/lib/index.js
./app/api/pg/route.js
...


How can I use pg in a Next.js project that I want to deploy to Cloudflare Pages?
Was this page helpful?