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

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

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 15 (2 by maintainers)

Most upvoted comments

I’m using Vite and this worked for me:

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

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)

nuxt.config.ts -> nitro

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

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.

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’.