Skip to content

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

@noook

Description

@noook

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions