db0 icon indicating copy to clipboard operation
db0 copied to clipboard

Postgres connector: The requested module 'pg' does not provide an export named 'Client'

Open noook opened this issue 11 months ago • 3 comments

Environment

Node.js: Reproducible with v18.18.0 and v21.6.2

[email protected] [email protected]

Reproduction

https://stackblitz.com/edit/github-xvw4kq?file=package.json

Describe the bug

Given the following configuration:

import { defineNitroConfig } from 'nitropack/config';

export default defineNitroConfig({
  srcDir: 'server',
  experimental: {
    database: true,
  },
  database: {
    default: {
      connector: 'postgresql',
      options: {
        url: '',
      },
    },
  },
});

The server fails to start because the Client import from "pg" does not seem to be exported.

Additional context

I'm not sure what's happening here, but I can run this with no issue.

// nitro.config.ts
import { Client } from "pg"
console.log(Client) // Works 👍🏻 

I tried integrating without nitro's useDatabase with pg, and I am obliged to instantiate the client this way:

// server/utils/db.ts
import { drizzle, NodePgDatabase } from 'drizzle-orm/node-postgres';
import pg from 'pg' // <-
import * as schema from '~/server/database/schemas'

let _db: NodePgDatabase<typeof schema> | null = null

export async function useDatabase() {
  if (!_db) {
    const client2 = new pg.Client({ // <-
      connectionString: import.meta.env.DATABASE_URL,
    })
    await client2.connect()
    _db = drizzle(client2, {
      schema,
    })
  }

  return _db
}

Logs

ERROR  [worker reload] [worker init] The requested module 'pg' does not provide an export named 'Client'                                                                                                                                                                                                          2:03:46 PM

  import { Client } from "pg";
  ^^^^^^
  SyntaxError: The requested module 'pg' does not provide an export named 'Client'
  at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
  at async ModuleJob.run (node:internal/modules/esm/module_job:214:5)
  at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
  at async loadESM (node:internal/process/esm_loader:28:7)
  at async handleMainPromise (node:internal/modules/run_main:120:12)

noook avatar Mar 10 '24 13:03 noook