canirequire › Dual (CommonJS + ESM) › pg
Is pg ESM-only? Can you require() it?
PostgreSQL client - pure javascript & libpq with the same API
Last verified Sep 9, 2026 against pg@8.23.0. Markdown · JSON
Dual (CommonJS + ESM) Dual package — require() works on every Node.js version we test; import works too
pg publishes both a CommonJS entry and a native ESM entry (ESM added in 8.15.0). require("pg") works on every Node.js version we test (18, 20, 22, 24 and 26) and in Bun. The package declares Node.js >= 16.0.0. import "pg" works alongside it.
What to do
- Nothing to change. require("pg") works on every Node.js version we test (18, 20, 22, 24 and 26) and in Bun. The package declares Node.js >= 16.0.0. import * as pg from "pg" works alongside it; use named imports for what the ESM entry exports.
- TypeScript: npm install -D @types/pg
Does pg@8.23.0 actually load? Runtime test results
Installed and executed on Sep 9, 2026 with require("pg") from a CommonJS file and import "pg" from an ES module.
| Runtime | require("pg") | import "pg" |
|---|---|---|
| Node.js 18 | ✓ works | ✓ works |
| Node.js 20 | ✓ works | ✓ works |
| Node.js 22 | ✓ works | ✓ works |
| Node.js 24 | ✓ works | ✓ works |
| Node.js 26 | ✓ works | ✓ works |
| Bun 1.4.0 | ✓ works | ✓ works |
How to import pg
ES modules (import)
import * as pg from "pg";Namespace import shown because the ESM entry may have no default export; use named imports for what it exports.
CommonJS (require)
const pg = require("pg");Module format by version
Derived from the type, main and exports fields of every stable release on npm, corrected where a runtime test of that version contradicted them (246 versions; the newest 60 shown).
| Versions | Format | First published |
|---|---|---|
| 8.15.0 – 8.23.0 | Dual (CommonJS + ESM) | Apr 22, 2025 |
| 7.17.0 – 8.14.1 | CommonJS | Dec 29, 2019 |
Frequently asked
Is pg ESM-only?
pg@8.23.0 ships CommonJS and ESM builds. pg publishes both a CommonJS entry and a native ESM entry (ESM added in 8.15.0). require("pg") works on every Node.js version we test (18, 20, 22, 24 and 26) and in Bun. The package declares Node.js >= 16.0.0. import "pg" works alongside it.
Can I require pg?
Yes. pg ships a CommonJS entry alongside the native ESM entry. require("pg") works on every Node.js version we test (18, 20, 22, 24 and 26) and in Bun. The package declares Node.js >= 16.0.0.
Does pg work in Bun?
Yes. import "pg" loaded successfully in Bun 1.4.0 on Sep 9, 2026.
Does pg ship TypeScript types?
Not bundled. Install the community types with "npm install -D @types/pg".
Links
Other dual (commonjs + esm) packages
- minimatch 10.2.6Dual
- brace-expansion 5.0.9Dual
- lru-cache 11.5.2Dual
- tslib 2.8.1Dual
- glob 13.0.6Dual
- eslint-visitor-keys 5.0.1Dual
- yallist 5.0.0Dual
- balanced-match 4.0.4Dual
Similar popularity
- is-boolean-object 1.2.2CommonJS
- typed-array-buffer 1.0.3CommonJS
- react-router 8.3.1ESM-only
- string-length 7.0.1ESM-only
- supports-preserve-symlinks-flag 1.0.0CommonJS
- jest-snapshot 30.5.1Dual
- core-js-compat 3.50.0CommonJS
- has-bigints 1.1.0CommonJS