Skip to content
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

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

Closed
noook opened this issue Mar 10, 2024 · 3 comments · Fixed by #63
Closed

Comments

@noook
Copy link
Contributor

noook commented Mar 10, 2024

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)
@amandesai01
Copy link
Contributor

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
Copy link
Contributor

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

@alex-eri
Copy link

alex-eri commented Mar 20, 2024

Seems esm imports not work at all in pg. brianc/node-postgres#3060 brianc/node-postgres#2534

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

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

Successfully merging a pull request may close this issue.

3 participants