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

support esm #3060

Open
its-dibo opened this issue Sep 6, 2023 · 14 comments · May be fixed by #3407
Open

support esm #3060

its-dibo opened this issue Sep 6, 2023 · 14 comments · May be fixed by #3407

Comments

@its-dibo
Copy link

its-dibo commented Sep 6, 2023

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

brianc commented Sep 6, 2023

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

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

brianc commented Sep 17, 2023

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

@rubiagatra
Copy link

I will take a look @brianc maybe in this weekend

@aegatlin
Copy link

aegatlin commented Oct 5, 2023

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

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

nice <3

@mahnunchik
Copy link

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

EspadaV8 added a commit to EspadaV8/kysely that referenced this issue Dec 19, 2023
Update pg import for CommonJS

pg package does not support ESM so the imports need to handle CommonJS

See: <brianc/node-postgres#3060>
@nezi311
Copy link

nezi311 commented Jul 13, 2024

FFS 07.2024 and the docs are stil not changed.

@brianc
Copy link
Owner

brianc commented Jul 13, 2024

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.

@mesqueeb
Copy link

@brianc you might not have experienced issues if your projects build to commonJS, or you do build to ESM but you bundle the entire pg library.

IF you try to run the project in ESM without a bundler converting your source code, this runtime error will happen.

It's verifiable here as well:
https://arethetypeswrong.github.io/?p=pg%408.14.1

The docs are inaccurate in that when running in an ESM environment, named imports of Pg won't work.
Only this would work:

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

the solution is that you define an exports field in your package.json. This will make your package available for both CJS and ESM environments. This can be done without other source code changes.

I could open a PR to do so if you agree with this solution. What do you think?

@brianc
Copy link
Owner

brianc commented Mar 19, 2025

I could open a PR to do so if you agree with this solution. What do you think?

if all it is is a package.json thing? abso-luuutely open to that! Last time I looked at building both esm and commonjs from the same codebase (granted this was like a year ago) my face melted at the complexity!

@mikavilpas
Copy link

I'm just a bystander here, but I have had a good experience with the https://arethetypeswrong.github.io/ tool. Things are made a lot simpler by it - basically if it says everything is ok (checkable with attw --pack), things are looking good.

My codebase at work is also affected by this, so maybe I can offer additional testing.

@mesqueeb mesqueeb linked a pull request Mar 19, 2025 that will close this issue
@mesqueeb
Copy link

@mikavilpas can you test my PR?

@mikavilpas
Copy link

Hmm I looked into this, but I'm not sure if this is the same error at all.

import { Pool } from 'pg'
         ^

SyntaxError: The requested module 'pg' does not provide an export named 'Pool'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:180:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:263:5)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:547:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:116:5)

Node.js v22.13.1

Maybe I have a different error. Hopefully someone else can help test it.

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.

8 participants