brianc / node-postgres

PostgreSQL client for node.js.
https://node-postgres.com
MIT License
12.36k stars 1.23k forks source link

Error during build because of pg cloudflare #2987

Open Feuerhamster opened 1 year ago

Feuerhamster commented 1 year ago

We're using the pg package in a project. Recently, we get this error during build process:

error   [vite]: Rollup failed to resolve import "cloudflare:sockets" from "/node_modules/pg-cloudflare/dist/index.js".
  This is most likely unintended because it can break your application at runtime.
  If you do want to externalize this module explicitly add it to
  `build.rollupOptions.external`
Error: [vite]: Rollup failed to resolve import "cloudflare:sockets" from "/node_modules/pg-cloudflare/dist/index.js".

The thing is, we are not using cloudflare or cloudflare workers at all. I saw that the pg-cloudflare package is in the optional dependencies of the pg package. For whatever reason, node installs all optional dependencies by default. So I tried installing the packages without the optional dependencies.

But now there is this error:

✘ [ERROR] Could not resolve "pg-cloudflare"

    node_modules/pg/lib/stream.js:10:41:
      10 │     const { CloudflareSocket } = require('pg-cloudflare')
         ╵                                          ~~~~~~~~~~~~~~~

  You can mark the path "pg-cloudflare" as external to exclude it from the bundle, which will remove
  this error. You can also surround this "require" call with a try/catch block to handle this
  failure at run-time instead of bundle-time.

What did I do wrong?

charmander commented 1 year ago

2978 will supposedly improve this. But like the error message says,

You can mark the path "pg-cloudflare" as external to exclude it from the bundle, which will remove this error.

Feuerhamster commented 1 year ago

This fixed the problem during build but at runtime with client side components it still appears

charmander commented 1 year ago

Your client-side components shouldn’t be loading pg; even if you can get it to quietly import without errors, that’s a lot of unnecessary code served to clients.