brianc / node-postgres

PostgreSQL client for node.js.

Home Page:https://node-postgres.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

difficulty with typescript - classes not being properly recognized for some reason using @types/pg

DaveTorrey opened this issue · comments

I have the weirdest problem. I've used pg fine with regular javascript when setting 'module' in package.json. however, when using typescript and @types/pg, I get the following error:

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:

what's really weird is i can see in the index.d.ts that it properly exports the Pool class, but I can't figure out what exactly is going on.

I've tried the import * as pg method i've seen elsewhere, but then on let x = new pg.Pool() it complains that pg.Pool is not a constructor.

CommonJS modules can always be imported via the default export

Using the default export would be import pg from 'pg'.

You can import pg and whatever types and classes you need like so:
import pg, { Connection, PoolClient } from 'pg';

Thanks, that did it. No idea why i struggled with that