Example failed to run: `pg` does not provide an export named `Pool`

I'm trying to run the example from the README file. I made a minial version, installed pg, @types/pg, drizzle-orm and drizzle-kit and tried to run it via tsx. This is my stripped down example:
import { drizzle } from "drizzle-orm/node-postgres";
import {
InferModel,
pgTable,
serial,
text,
timestamp,
} from "drizzle-orm/pg-core";
import { Pool } from "pg";

export const domains = pgTable("domains", {
id: serial("id").primaryKey(),
domain: text("domain").notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull(),
});

export type Domain = InferModel<typeof domains>;
export type NewDomain = InferModel<typeof domains, "insert">;

const pool = new Pool({
connectionString: "postgres://robin@localhost/ts-drizzle",
});
const db = drizzle(pool);

async function main() {
// Insert
const d: NewDomain = {
domain: "foo.com",
};
const inserted = await db.insert(domains).values(d).returning();
console.log(inserted);
}

main();
import { drizzle } from "drizzle-orm/node-postgres";
import {
InferModel,
pgTable,
serial,
text,
timestamp,
} from "drizzle-orm/pg-core";
import { Pool } from "pg";

export const domains = pgTable("domains", {
id: serial("id").primaryKey(),
domain: text("domain").notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull(),
});

export type Domain = InferModel<typeof domains>;
export type NewDomain = InferModel<typeof domains, "insert">;

const pool = new Pool({
connectionString: "postgres://robin@localhost/ts-drizzle",
});
const db = drizzle(pool);

async function main() {
// Insert
const d: NewDomain = {
domain: "foo.com",
};
const inserted = await db.insert(domains).values(d).returning();
console.log(inserted);
}

main();
This is my tsconfig.json:
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"strict": true
}
}
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"strict": true
}
}
Running it:
$ npx tsx main.ts
SyntaxError: The requested module 'pg' does not provide an export named 'Pool'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:533:24)
at async loadESM (node:internal/process/esm_loader:91:5)
at async handleMainPromise (node:internal/modules/run_main:65:12)
$ npx tsx main.ts
SyntaxError: The requested module 'pg' does not provide an export named 'Pool'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:533:24)
at async loadESM (node:internal/process/esm_loader:91:5)
at async handleMainPromise (node:internal/modules/run_main:65:12)
5 Replies
Dan
Dan2y ago
Weird. Could you try running it with esbuild-register or ts-node? Also, is your project in ESM mode? Also, doesn't seem to be related to Drizzle, cause the error come from pg.
roob
roobOP2y ago
@bloberenober I removed the "type": "module" now from my package.json and it seems to solve this, thanks! However I do not understand why this error occured because of this Do you have a link or keyword I can google to understand this behaviour?
Dan
Dan2y ago
Well, it's related to ESM support in general. A lot of libraries are only built with CommonJS support and thus are incompatible with ESM. You can just search for ESM vs CommonJS in general, I guess.
roob
roobOP2y ago
thanks, will do this!
NovemberSpawn
NovemberSpawn12mo ago
try
import pg from 'pg'

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

const { Pool } = pg
const pool = new Pool()
Want results from more Discord servers?
Add your server