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

Documentation examples not running #3190

Closed
nboisnea opened this issue Apr 4, 2024 · 6 comments
Closed

Documentation examples not running #3190

nboisnea opened this issue Apr 4, 2024 · 6 comments

Comments

@nboisnea
Copy link

nboisnea commented Apr 4, 2024

It seems this has been asked a few times already but I cannot find a proper answer.
How are the examples from the documentation supposed to be run?

Looking at the code in the Getting started section:

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()

Is it JavaScript? TypeScript? There is no indication. If I try running this script with Node I get an error saying pg is a CommonJS module:
SyntaxError: Named export 'Client' not found. The requested module 'pg' is a CommonJS module, which may not support all module.exports as named exports.

I know the answer is to use the default export, I just wonder what is the reason for not specifying it in the documentation. And if there is a way to run this code as-is, I feel like it should at least be specified in the documentation.

@brianc
Copy link
Owner

brianc commented Apr 4, 2024 via email

@sven-borkert
Copy link

sven-borkert commented Apr 5, 2024

So what does this actually mean? I'm trying to get this lib working in a Nuxt3 file, and I'm having similar issues. I thought this is the official Postgres driver, but just completely fail using it.

grafik

(Sorry if this is a stupid question. I'm trying to learn this stuff and I'm a total newbie to web applications.)

@charmander
Copy link
Collaborator

@sven-borkert (It’s not “official”, just the most popular PostgreSQL client for Node.) See the duplicate:

@charmander
Copy link
Collaborator

Duplicate of #3096

@charmander charmander marked this as a duplicate of #3096 Apr 5, 2024
@charmander charmander closed this as not planned Won't fix, can't repro, duplicate, stale Apr 5, 2024
@brianc
Copy link
Owner

brianc commented Apr 5, 2024

So what does this actually mean?

It means I have been maintaining this library with some lovely help from other volunteers for 13-14 years and throughout that time ESM modules (technically ECMAScript Modules -- the import { Client } from 'pg') style syntax has only existed for 3-4 of those years as a standard, IIRC? Historically you would import the code with const pg = require('pg') or const { Client } = require('pg') which uses an entirely different module system (called commonJS modules). At some point within the past year someone did a PR which "cleaned up" some of the documentation & accidentally changed the import syntax to that of ESM. I have it on my plate very soon to both update the documentation and add support for ESM modules. It will take a bit of fiddling on my part as I want to support both ESM AND commonJS (to not break backwards compatibility). In the mean time, hang tight and import it like this:

import pg from 'pg'
const { Client } = pg

That should work. That's how you currently import commonJS modules into an ESM environment.

This is probably one of the things folks would point to about JavaScript being "a mess" from other programming languages. Its a beautiful mess though because of its strong devotion to backwards compatibility old code from 10 years ago still has to work and so you end up w/ sometimes awkward and multi-year long transitions like this. And then those transitions run up against a small team (or single person) maintainers of packages and those folks have real life jobs & families to take care of & you get delays like this. 😅 It WILL be fixed soon though. If you think it's useful I can update the docs in the meantime.

@nboisnea
Copy link
Author

nboisnea commented Apr 5, 2024

@brianc No worries about the docs if ESM is about to be fully supported. Hopefully people will find this issue with your explanation in the meantime. Thank you so much for your amazing work on this library!

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

No branches or pull requests

4 participants