canirequire

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

  1. 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.
  2. TypeScript: npm install -D @types/pg
Latest
8.23.0 (Aug 8, 2026)
Module format
Dual (CommonJS + ESM)
Last CommonJS version
8.23.0
Native ESM since
8.15.0
TypeScript types
@types/pg
Node engines
>= 16.0.0
Weekly downloads
46.3M
License
MIT

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.

Runtimerequire("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).

VersionsFormatFirst published
8.15.0 – 8.23.0Dual (CommonJS + ESM)Apr 22, 2025
7.17.0 – 8.14.1CommonJSDec 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

Similar popularity