node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

how to properly import pg into my ts file?

Open ash-hashtag opened this issue 4 years ago • 20 comments

I've tried this

import {Client} from "pg";

throws Named export 'Client' 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:

import pkg from 'pg';
const { Client } = pkg; //or// const Client = pkg.Client

and so following this, throws "there is no default export"

and

import * as pg from "pg";
const Client = pg.Client;
const sql = new Client(DB_URL); // or // const sql = new pg.Client(DB_URL);

throws Client is not a constructor

what should I do?

my tsconfig.json

{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "node",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "ES2017",
    "baseUrl": "./src"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

my package.json

{
 "scripts": {
    "build": "tsc",
  },
  "engines": {
    "node": "16"
  },
  "main": "lib/index.js",
  "dependencies": {
    "pg": "^8.8.0"
  },
  "devDependencies": {
    "@types/pg": "^8.6.5",
    "typescript": "^4.5.4"
  },
  "private": true,
  "type": "module"
}

Thank you

ash-hashtag avatar Sep 23 '22 04:09 ash-hashtag

@ash-hashtag Did you manage to find a work around for this issue? I also am having a related issue where it says pool.query is not a constructor.

raulbarriga avatar Oct 05 '22 04:10 raulbarriga

how did you solve the issue? I recently face similar issue, previously it is fine to directly import Pool. I am not sure where the problem is. I think it is connected to the fact that now I use pg in sveltekit with vite as the bundler. Its running dev env with no error but failed when I build the project

IqbalLx avatar Jun 02 '23 03:06 IqbalLx

I was able to make it run with

import pg from 'pg';
...

const dbClient = new pg.Client({
 ...

lfarroco avatar Jun 04 '23 19:06 lfarroco

I was getting the same error with:

import { Pool } from 'pg';
const pool = new Pool();

Got fixed by updating it to:

import pg from 'pg';
const pool = new pg.Pool()

jramiroz98 avatar Sep 08 '23 15:09 jramiroz98

I was getting the same error with:

import { Pool } from 'pg';
const pool = new Pool();

Got fixed by updating it to:

import pg from 'pg';
const pool = new pg.Pool()

Nice one

leogiouldasis avatar Oct 23 '23 13:10 leogiouldasis

Why don't you just do what the error message tells you?

import pg from "pg";
const { Pool } = pg;

mekterovic avatar Oct 23 '23 14:10 mekterovic

To anyone that finds this, you need to set esModuleInterop in tsconfig to true.


{
  "compilerOptions": {
    // ..
    "esModuleInterop": true,
  },
   // ...
}

tenkoverse avatar Feb 29 '24 06:02 tenkoverse

I'll be adding better support for es modules soon!

brianc avatar Feb 29 '24 16:02 brianc

import { Pool } from 'pg'

so it was fake? @brianc

BIMASKUY avatar Mar 02 '24 11:03 BIMASKUY

I think this Pool matter should come to an end, I couldn't import them properly and throwing consistent errors, and I just moved to commonJS that's the only option I had to work with pg

ghost avatar Mar 23 '24 21:03 ghost

nothing works here, same issue, can't import { Client } from 'pg'; in nuxt3

flyingkaktus avatar Mar 24 '24 14:03 flyingkaktus

I was surprised too. I didn't use typescript, just a vanilla es6 module. The import statement in the documentation is not working at all which is strange.

tluanga34 avatar Mar 25 '24 01:03 tluanga34

Thanks for the feedback! I will be looking into this next month once my job wraps up. Should have it fixed for ya then. Sorry for the headache 🫠

brianc avatar Mar 25 '24 01:03 brianc

Has this been resolved? The issue was closed two years ago, and yet I am still having the same issue.

kesor avatar Jun 20 '24 22:06 kesor

What issue? Can you show me a code example? I use pg from my typescript every day.

On Thu, Jun 20, 2024 at 5:44 PM Evgeny Zislis @.***> wrote:

Has this been resolved? The issue was closed two years ago, and yet I am still having the same issue.

— Reply to this email directly, view it on GitHub https://github.com/brianc/node-postgres/issues/2819#issuecomment-2181667984, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAMHIMUWZ4CZMALGWZW753ZINLMRAVCNFSM6AAAAAAQTUH7EOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRGY3DOOJYGQ . You are receiving this because you commented.Message ID: @.***>

brianc avatar Jun 21 '24 00:06 brianc

What issue? Can you show me a code example? I use pg from my typescript every day.

 Exception during run: file:///app/packages/postgres/dist/src/index.js:1
import { Pool } from 'pg';
         ^^^^
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:

import pkg from 'pg';
const { Pool } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:171:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:254:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:474:24)
    ...

kesor avatar Jun 21 '24 14:06 kesor

What worked for me:

import pg from "pg";
const { Pool } = pg;

and then installing the types : pnpm i @types/pg

geekodour avatar Jul 04 '24 08:07 geekodour

@geekodour if this would have "just worked" intuitively (i.e. as you expect everything else to work) you would not have banged your head against the wall for an hour to finally find the solution and come here to this PR and tell us about it. You would have just done import { Pool } from 'pg' and it would have worked, and you wouldn't have even thought of going to GitHub. That is the issue.

kesor avatar Jul 05 '24 13:07 kesor

Hi, any updates on this?

The docs (https://node-postgres.com/apis/client) state:

"... example to create a client with specific connection information:

import { Client } from 'pg' ..."

But this leads to:

import { Client } from "pg";
         ^^^^^^
SyntaxError: Named export 'Client' 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:

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

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:220:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:321:5)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:644:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:117:5)

Node.js v22.17.1

HeinzContiZacherl avatar Aug 10 '25 10:08 HeinzContiZacherl

@HeinzContiZacherl Which version of pg do you have installed?

charmander avatar Aug 10 '25 18:08 charmander