Skip to content

node_modules/pg/lib/native/client.js:4:21: ERROR: Could not resolve "pg-native" #2800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gdonoso94 opened this issue Aug 31, 2022 · 15 comments
Closed

Comments

@gdonoso94
Copy link

Hi,

I'm receiving this error when I run my code

node_modules/pg/lib/native/client.js:4:21: ERROR: Could not resolve "pg-native"

Do I need to install pg-native? When I did the tests started crashing with FATAL ERROR, with something related to v8...

Is there any alternative here? I'm using pg Pool to make simple queries and typeorm as orm

@brianc
Copy link
Owner

brianc commented Sep 1, 2022

You need to install pg-native if you want to use the native bindings. Otherwise just don't require the native bindings and use the pure JS version (which is plenty fast for production workloads). As far as what fatal error you were getting with v8 I'd need more info to debug:

  1. OS you're on
  2. version of node
  3. self-contained script to reproduce the problem

@gdonoso94
Copy link
Author

Hi brianc,

It looks like it has something to do with the bundler (I'm using Remix.run). I'm trying to solve it on that end.

Regarding the v8:

  • MacOs Monterey 12.5.1
  • 18.4

I can't share the scripts nor the repo since it's private...

@composite
Copy link

Hi @brianc
I have same issue. I can't run dev or build due to same error on same OS, node 16.14.
Here is my log.

Should I install pg-native for resolve this error?

https://jsfiddle.net/composite/fm2c5eg3/embedded/result/

> vite dev


  VITE v3.0.2  ready in 558 ms

  ➜  Local:   http://127.0.0.1:3101/
  ➜  Network: use --host to expose
✘ [ERROR] Could not resolve "pg-native"

    ../../node_modules/.pnpm/[email protected]/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.

/projects/dazare/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1605
  let error = new Error(`${text}${summary}`);
              ^

Error: Build failed with 1 error:
../../node_modules/.pnpm/[email protected]/node_modules/pg/lib/native/client.js:4:21: ERROR: Could not resolve "pg-native"
    at failureErrorWithLog (/projects/dazare/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1605:15)
    at /projects/dazare/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1251:28
    at runOnEndCallbacks (/projects/dazare/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1034:63)
    at buildResponseToResult (/projects/dazare/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1249:7)
    at /projects/dazare/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1358:14
    at /projects/dazare/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:666:9
    at handleIncomingPacket (/projects/dazare/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:763:9)
    at Socket.readFromStdout (/projects/dazare/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:632:7)
    at Socket.emit (node:events:526:28)
    at addChunk (node:internal/streams/readable:315:12) {
  errors: [
    {
      detail: undefined,
      id: '',
      location: {
        column: 21,
        file: '../../node_modules/.pnpm/[email protected]/node_modules/pg/lib/native/client.js',
        length: 11,
        line: 4,
        lineText: "var Native = require('pg-native')",
        namespace: '',
        suggestion: ''
      },
      notes: [
        {
          location: null,
          text: '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.'
        }
      ],
      pluginName: '',
      text: 'Could not resolve "pg-native"'
    }
  ],
  warnings: []
}
 ELIFECYCLE  Command failed with exit code 1.

@brianc
Copy link
Owner

brianc commented Sep 2, 2022

If you're using a bundler that's likely the problem. Native code doesn't bundle well. Just use the pure JS bindings - I use them currently in prod and they're very fast.

@brianc brianc closed this as completed Sep 2, 2022
@Lilja
Copy link

Lilja commented Oct 31, 2022

We're also running into this issue, we've imported pg as

import { Client } from "pg";

and we're bundling through esbuild:
esbuild ./src/ingest.ts --bundle --platform=node --outdir=dist

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

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

EDIT: We externalized the pg-native requirement(--external:pg-native for esbuild)

@ericmorand
Copy link

@brianc , I'm facing the same issue, and I'd like to test your solution:

If you're using a bundler that's likely the problem. Native code doesn't bundle well. Just use the pure JS bindings - I use them currently in prod and they're very fast.

But I don't know what you mean by "Just use the pure JS bindings". I'm not doing anything specific that would make me use the non-pure JS bindings.

Can you please elaborate what you mean and how we can use the pure JS bindings? Thanks.

@mrcndn
Copy link

mrcndn commented Mar 18, 2023

I'm also getting this error,
not that we do not specify that native or js will be used. we only import 'pg'.

the problem is the code automatically tries to import pg-native which does not exist. We cannot use pg-native in serverless platforms. So maybe Client and NativeClient should be separate then webpack will not try to include 'pg-native'.

@ericmorand
Copy link

the problem is the code automatically tries to import pg-native which does not exist.

Are you using an ORM package, like TypeORM?

@mrcndn
Copy link

mrcndn commented Mar 18, 2023

the problem is the code automatically tries to import pg-native which does not exist.

Are you using an ORM package, like TypeORM?

No, I'm using Cloudflare workers with TypeScript. When trying to run the code as I understand it bundles the code with webpack. And because libpq is not installed on my machine (also in the cloud) pg-native cannot be installed too. So I get this error.

@ericmorand
Copy link

ericmorand commented Mar 19, 2023

I meant, in what context to you use PG? I know for example that, when using TypeORM, you can - and you should IMHO - force the driver that you want to use and that it default to pg-native.

@charmander
Copy link
Collaborator

charmander commented Mar 19, 2023

@mrcndn You might also need to configure your bundler to ignore pg-native (see people setting it as external for ESBuild above and for Webpack at #838 (comment), for example).

@ericmorand
Copy link

ericmorand commented Mar 20, 2023

It would be great if libs stopped doing dynamic - and implicit - imports once and for all so that we don't have to guess their internals just to build an application. People that want to use pg-native should have to import the package explicitely and pass it as configuration to the library.

@Sahasrara
Copy link

I'm using Vite and this worked for me:

        build: {
            rollupOptions: {
                external: ['pg-native'],
            },
        },

@MickL
Copy link

MickL commented Dec 6, 2024

For Nuxt / Nitro this did the trick:

rollupConfig: {
    external: ['pg-native'],
  },

BUT next error I get is:

Cannot resolve "cloudflare:sockets" from "/Users/mick/git/.../node_modules/pg-cloudflare/dist/index.js" and externals are not allowed!

So I also added 'cloudflare:sockets' to externals, too.

@aqz236
Copy link

aqz236 commented Jan 8, 2025

nuxt.config.ts -> nitro

  nitro: {
    rollupConfig: {
      external: ['pg-native', 'cloudflare:sockets'],
    },
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants