Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I import the node-postgres module (pg) or is it CommonJS only? [duplicate]

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.

like image 587
SuperCatchyUsername Avatar asked Mar 27 '19 12:03

SuperCatchyUsername


1 Answers

This should handle that I think.

import * as pg from 'pg'
const { Pool } = pg
like image 97
PrivateOmega Avatar answered Sep 21 '22 20:09

PrivateOmega