node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

support esm

Open its-dibo opened this issue 2 years ago • 14 comments

when running this package inside an ESM project, I got this error

import { Pool } from 'pg';
         ^^^^
SyntaxError: Named export 'Pool' not found. The requested module 'pg' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'pg';
const { Pool } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

using the suggested form import pkg from 'pg'; disables the tree-shaking

its-dibo avatar Sep 06 '23 06:09 its-dibo

fwiw there is very, very little (if any) code that would be removed with tree-shaking if you are importing the pool. It literally depends on all the code in the project.

brianc avatar Sep 06 '23 17:09 brianc

is the documentation wrong? @brianc https://node-postgres.com/

import { Client } from 'pg'
const client = new Client()
await client.connect()
 
const res = await client.query('SELECT $1::text as message', ['Hello world!'])
console.log(res.rows[0].message) // Hello world!
await client.end()

rubiagatra avatar Sep 17 '23 01:09 rubiagatra

I'm open to a pull request to add ESM support so long as it maintains backwards compatibility.

brianc avatar Sep 17 '23 05:09 brianc

I will take a look @brianc maybe in this weekend

rubiagatra avatar Sep 19 '23 04:09 rubiagatra

Hello, and thank you for the library @brainc <3

I got to this issue because the docs imply this type of import is supported here: https://node-postgres.com/apis/pool

import { Pool } from 'pg'
 
const pool = new Pool({
  host: 'localhost',
  user: 'database-user',
  max: 20,
  idleTimeoutMillis: 30000,
  connectionTimeoutMillis: 2000,
})

Should the docs be updated to the following?

import pg from 'pg'
 
const pool = new pg.Pool({
  host: 'localhost',
  user: 'database-user',
  max: 20,
  idleTimeoutMillis: 30000,
  connectionTimeoutMillis: 2000,
})

aegatlin avatar Oct 05 '23 05:10 aegatlin

I'm open to a pull request to add ESM support so long as it maintains backwards compatibility.

nice <3

its-dibo avatar Oct 12 '23 14:10 its-dibo

Why is the documentation written in a way that clearly doesn’t work?

mahnunchik avatar Nov 16 '23 22:11 mahnunchik

FFS 07.2024 and the docs are stil not changed.

nezi311 avatar Jul 13 '24 14:07 nezi311

are the docs inaccurate? If so it would be helpful if you would point out where. I went through a while ago and tried to make them all clear and correct. You could also do a PR with the changes if that's easier.

brianc avatar Jul 13 '24 15:07 brianc