15

I have been writing most of my APIs in old-school JavaScript using const var = require('var'). I am now writing my first API in ES6 syntax including using import rather than require. I always use the node-postgres module const {Pool} = require('pg') but when I try to write this as import {Pool} from 'pg' I get the error

SyntaxError: The requested module 'pg' does not provide an export named 'Pool'.

Similarly, import Pool from 'pg' gives me

TypeError: Pool is not a constructor

Is there a way to import this as an ES6 module or do I need to find another package for my ES6 postgres connections? I couldn't find any examples online of people using node-postgres with imports.

404
  • 8,022
  • 2
  • 27
  • 47
SuperCatchyUsername
  • 375
  • 1
  • 5
  • 14

1 Answers1

26

This should handle that I think.

import * as pg from 'pg'
const { Pool } = pg
PrivateOmega
  • 2,509
  • 1
  • 17
  • 27
  • Your answer and the answer to the question that my question was apparently a duplicate of were both correct/solved my problem, but the other question was a more thorough answer. Thanks! – SuperCatchyUsername Apr 01 '19 at 11:34
  • If you are using Typescript strict mode, make sure you provide the `allowSyntheticDefaultImports` compiler option – Urigo Jan 17 '20 at 19:11
  • 9
    `const { Pool } = pg.default` – Parker Nov 16 '21 at 14:31
  • 4
    If you use `import pg from 'pg'` instead of `import * as pg from 'pg'`, you can use `const { Pool } = pg` instead of `const { Pool } = pg.default` – some Nov 11 '22 at 03:09
  • 1
    This gives an error `Property 'Pool' does not exist on type 'typeof import("/home/server/pg")'. const { Pool } = pg`, even with allowSyntheticDefaultImports. – sryscad Jan 18 '23 at 11:07