unjs / db0

πŸ“š Lightweight SQL Connector
https://db0.unjs.io
MIT License
153 stars 15 forks source link

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

Open noook opened 6 months ago

noook commented 6 months ago

Environment

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

db0@0.1.4 pg@8.11.3

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)
amandesai01 commented 6 months ago

As I can see, Nitro's build output is in *.mjs, due to which, it uses import rather than require.

DB0 outputs both mjs and cjs files. cjs file's build is updated to handle the case in build process and has var _pg = require("pg"); const client = new _pg.Client(....

On the other hand, import statement uses destructured imports which is causing the issue.

This might be because, pg was not written to work with esm.

amandesai01 commented 6 months ago

After running above fix on the provided reproduction, I can verify that the issue gets resolved.

alex-eri commented 6 months ago

Seems esm imports not work at all in pg. https://github.com/brianc/node-postgres/issues/3060 https://github.com/brianc/node-postgres/pull/2534

ClientConfig also not imports in my project. Same env as TS, but on Nuxt.