0

Why does this work:

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

and this doesn't:

import { Client } from 'pg'

When I try:

import { Client } from 'pg'

I get this error:

enter image description here

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Does this answer your question? [Importing CommonJS does not destructure correctly in ES6](https://stackoverflow.com/questions/53546863/importing-commonjs-does-not-destructure-correctly-in-es6) – pilchard Jun 20 '23 at 22:00
  • or [ES6 Destructuring and Module imports](https://stackoverflow.com/questions/33524696/es6-destructuring-and-module-imports) – pilchard Jun 20 '23 at 22:00
  • Actually they do answer your question, and the text of the error does as well. – pilchard Jun 20 '23 at 23:41
  • I know that I could use require or do it the way it's shown in the terminal, but what I want is to use import like this => import { Client } from 'pg', because in their documentation, they do the same. I just don't understand why I can't. @pilchard :/ (documentation: https://node-postgres.com/) – samyss.dev Jun 21 '23 at 11:48
  • Looks like even the most recent version is CommonJS. If you're using Typescript you can enable `allowSyntheticDefaultImports` but otherwise you need to import and then destructure. (named imports *look* like destructuring but aren't) – pilchard Jun 21 '23 at 12:09
  • A more explicit duplicate: [Can I import the node-postgres module (pg) or is it CommonJS only?](https://stackoverflow.com/questions/55377103/can-i-import-the-node-postgres-module-pg-or-is-it-commonjs-only) – pilchard Jun 21 '23 at 12:11

0 Answers0