Registry / database / pg-error-enum

pg-error-enum

JSON →
library2.0.1jsnpmunverified

TypeScript Enum for Postgres Error Codes. Version 2.0.1 provides generated TypeScript enums mapping all PostgreSQL error codes to their SQLSTATE values, with zero runtime dependencies. Updated from the PostgreSQL errcodes.txt source. Supports ESM and CommonJS. Ideal for type-safe error handling in Node.js with the 'pg' library, eliminating magic strings.

npm install pg-error-enum
INSTALL
IMPORT
SIG · PG-ERROR-ENUM
P
pg-error-enum
databasejavascriptv2.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PostgresError
import { PostgresError } from 'pg-error-enum'
import PostgresError from 'pg-error-enum'
Only named export available; default import not supported.
PostgresError
const { PostgresError } = require('pg-error-enum')
const PostgresError = require('pg-error-enum')
CommonJS destructure required, as the package exports an object.
type PostgresError
import type { PostgresError } from 'pg-error-enum'
Type-only import possible when only using enum as type.

Shows how to import and use the PostgresError enum to handle PostgreSQL unique constraint violations in a TypeScript project.

import { PostgresError } from 'pg-error-enum'; // Example with node-postgres import pg from 'pg'; const client = new pg.Client({ connectionString: process.env.DATABASE_URL ?? '' }); async function createUser(username: string) { try { await client.query('INSERT INTO users (username) VALUES ($1)', [username]); } catch (err: any) { if (err.code === PostgresError.UNIQUE_VIOLATION) { throw new Error('That username is taken'); } throw err; } }
Debug
Known issues
breakingVersion 2.0.0 dropped support for Node.js versions below 22.12.0
fix
Upgrade Node.js to ^22.12.0 || >=24.0.0
affects: <2.0.0
breakingEnum members renamed in v2; old names may be removed
fix
Check the generated enum for updated constant names; refer to the errcodes.txt mapping.
affects: >=2.0.0
gotchaEnum values are SQLSTATE strings (e.g., '23505'), not numeric codes.
fix
Compare with error.code from pg, which is a string.
affects: >=0.0.0
gotchaNot all PostgreSQL error codes may be present in older versions; ensure package is up-to-date.
fix
Upgrade to version 2.0.1 for latest codes.
affects: <2.0.0
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Code compiled as CommonJS while package uses ESM.
fix
Add "type": "module" to package.json or use dynamic import.
Module not found: Can't resolve 'pg-error-enum'
Package not installed or not in node_modules.
fix
Run: npm install pg-error-enum
TypeError: PostgresError is not a constructor
Importing class incorrectly; PostgresError is an enum, instantiation not needed.
fix
Use import { PostgresError } from 'pg-error-enum' and refer to members directly.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
4
Resources
pg-error-enum — npm install pg-error-enum · libregistry