how to properly import pg into my ts file?
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 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.
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
I was able to make it run with
import pg from 'pg';
...
const dbClient = new pg.Client({
...
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()
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
Why don't you just do what the error message tells you?
import pg from "pg";
const { Pool } = pg;
To anyone that finds this, you need to set esModuleInterop in tsconfig to true.
{
"compilerOptions": {
// ..
"esModuleInterop": true,
},
// ...
}
I'll be adding better support for es modules soon!
import { Pool } from 'pg'
so it was fake? @brianc
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
nothing works here, same issue, can't import { Client } from 'pg'; in nuxt3
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.
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 🫠
Has this been resolved? The issue was closed two years ago, and yet I am still having the same issue.
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: @.***>
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)
...
What worked for me:
import pg from "pg";
const { Pool } = pg;
and then installing the types : pnpm i @types/pg
@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.
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 Which version of pg do you have installed?