remix icon indicating copy to clipboard operation
remix copied to clipboard

[ERROR] Could not resolve "pg-native" when making single file build output

Open clintonwoo opened this issue 3 years ago • 5 comments

What version of Remix are you using?

1.3.5

Steps to Reproduce

Seems like ESBuild is throwing an error when using pg package and remix.config.js serverDependenciesToBundle: [/^.*$/] to produce a single file server build. The problem seems because the pg library uses lazily required natively built bindings that are only peer dependencies.

In remix 1.3.2 I can just modify the regex to avoid bundling it (serverDependenciesToBundle: [/^(?!pg-native).*$/]) but in remix 1.3.5 there's an additional error after about module onResolve from @remix-run/dev/compiler/plugins/serverBareModulesPlugin.js. Seems like the onResolve thing has been added after 1.3.2 at some point which is causing a problem with the unbundled module.

Marking pg-native as external (external: ['pg-native']) in ESBuild works as expected but Remix is not providing any API to modify ESBuild config.

Expected Behavior

The remix build should output the bundled build file without pg-native included since it doesn't exist in this context (it's a peer dependency and I'm not installing, building or using it).

Actual Behavior

Error msg:

Building Remix app in production mode...

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

    node_modules/pg/lib/native/client.js:4:21:
      4 │ var Native = require('pg-native')
        ╵                      ~~~~~~~~~~~

  You can mark the path "pg-native" 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.

clintonwoo avatar Apr 11 '22 21:04 clintonwoo

@clintonwoo there is an open PR https://github.com/remix-run/remix/pull/1841 to expose the esbuild externals via configuration :+1:

andrewbrey avatar Apr 11 '22 22:04 andrewbrey

@clintonwoo I think I might have a similar issue. Does it start working If you remove serverDependenciesToBundle entirely from your config?

garth avatar Apr 19 '22 10:04 garth

Hi

Same problem over here, is there any solution or workaround?

gdonoso94 avatar Sep 01 '22 07:09 gdonoso94

@gdonoso94 as you can see, there was a PR that went stale and got closed. The solution is to open a new PR to expose the esbuild externalsoption.

machour avatar Sep 01 '22 08:09 machour

Same issue here, is there any other workaround? By the way I also read this issue and renaming the file to {something}.server.ts does not help, I'm trying to use remix with cloudflare pages and still throws this error:

Building Remix app in production mode...

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

    node_modules/pg/lib/native/client.js:4:21:
      4 │ var Native = require('pg-native')
        ╵                      ~~~~~~~~~~~

  You can mark the path "pg-native" 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.

adrianiskandar avatar Oct 16 '22 04:10 adrianiskandar

I'm also seeing this issue trying to use cloudflare pages

ncrmro avatar Nov 17 '22 21:11 ncrmro

I just ran into this issue in Remix v1.9.0, but I solved it by renaming my database.ts file (which imports pg and calls top-level pg functions) to database.server.ts.

rob-johansen avatar Dec 31 '22 04:12 rob-johansen

I have also run into this issue. Unfortunately there seem to be a few issues with Prisma as well that prevents from using this in a single-file deployment (see these prisma errors):

  • https://github.com/prisma/prisma/issues/17630

Additionally with this error (pg) when trying to create a single-file deployment esbuild will error out as we cannot omit this pg-native package. I am not sure if there is another reliable way to connect to a postgres database in this case? Most of the other choices use node-postgres, I think unless we can omit this package we will not be able to connect to a postgres database in a single-file remix.run deployment:

  • https://github.com/brianc/node-postgres/issues/2800

This single-file deployment will probably be more popular as aRemixSite construct was released by SST that has been growing pretty quickly in popularity and creates single-file deployments.


After some further digging this appears to be an issue with SST's RemixSite construct. I have filed an issue here:

  • https://github.com/serverless-stack/sst/issues/2412

SpencerDuball avatar Feb 02 '23 02:02 SpencerDuball

I just ran into this issue in Remix v1.9.0, but I solved it by renaming my database.ts file (which imports pg and calls top-level pg functions) to database.server.ts.

Thank you @rob-johansen !

mateosantosdev avatar May 07 '23 10:05 mateosantosdev