brianc / node-postgres

PostgreSQL client for node.js.
https://node-postgres.com
MIT License
12.29k stars 1.23k forks source link

support esm #3060

Open its-dibo opened 1 year ago

its-dibo commented 1 year ago

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

brianc commented 1 year ago

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.

rubiagatra commented 1 year ago

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()
brianc commented 1 year ago

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

rubiagatra commented 1 year ago

I will take a look @brianc maybe in this weekend

aegatlin commented 1 year ago

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,
})
its-dibo commented 1 year ago

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

nice <3

mahnunchik commented 11 months ago

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

nezi311 commented 3 months ago

FFS 07.2024 and the docs are stil not changed.

brianc commented 3 months ago

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.