Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Retry
✓ const Retry = require('promised-retry')
Primary CommonJS import for Node.js environments. The package is explicitly CJS.
Retry
✓ import Retry from 'promised-retry'
✗ import { Retry } from 'promised-retry'
Correct ESM import for consuming the CommonJS default export. Avoid named imports as the package does not export a named 'Retry' symbol.
RetryOptions
✓ import type { RetryOptions } from 'promised-retry'
✗ import { RetryOptions } from 'promised-retry'
TypeScript type import for configuring the Retry constructor. This is a type-only import.
Demonstrates basic usage of `promised-retry` to establish and maintain a PostgreSQL database connection with automatic retries, custom delay settings, and graceful shutdown handling. It uses environment variables for database configuration for better security and flexibility.
const Retry = require('promised-retry');
const { Client } = require('pg');
const dbConfig = {
user: process.env.DB_USER ?? 'testuser',
host: process.env.DB_HOST ?? 'localhost',
database: process.env.DB_NAME ?? 'testdb',
password: process.env.DB_PASSWORD ?? 'testpassword',
port: parseInt(process.env.DB_PORT ?? '5432', 10),
};
let currentDbClient = null;
const channels = ['my_channel', 'another_channel'];
const retryInstance = new Retry({
name: 'PostgreSQL Connection',
try: async () => {
const db = new Client(dbConfig);
db.on('error', (err) => {
console.error('DB Client error, resetting retry:', err.message);
currentDbClient = null; // Mark client as invalid
retryInstance.reset();
// Optionally, if you want to immediately re-try after a connection error:
// retryInstance.try();
});
await db.connect();
console.log('Database connected successfully!');
return db; // Return the connected client
},
success: db => {
currentDbClient = db;
db.on('notification', (msg) => {
console.log(`Received notification on channel ${msg.channel}: ${msg.payload}`);
});
channels.forEach(channel => {
db.query('LISTEN ' + channel);
console.log(`Listening on channel: ${channel}`);
});
console.log('Successfully established DB connection and listeners.');
},
end: async db => {
if (db) {
console.log('Ending database connection...');
await db.end();
console.log('Database connection ended.');
}
},
retryMin: 1000, // minimum 1 second delay
retryBase: 1.5, // exponential backoff
retryExponent: 5 // max exponent for delay calculation
});
async function initializeDbConnection() {
try {
await retryInstance.try();
console.log('Initial database connection established.');
} catch (error) {
console.error('Failed to establish database connection after retries:', error.message);
}
}
initializeDbConnection();
// Example of how to end the retry mechanism (e.g., on application shutdown)
process.on('SIGINT', async () => {
console.log('Received SIGINT. Shutting down...');
await retryInstance.end(currentDbClient);
process.exit(0);
});
Errors
Common errors & fixes
TypeError: (0 , promised_retry_1.default) is not a constructor
Incorrect ESM import syntax for a CommonJS default export.
fixChange `import { Retry } from 'promised-retry';` to `import Retry from 'promised-retry';`. TypeError: Retry is not a constructor (when using named import in ESM)
Attempting to import `Retry` as a named export from a CommonJS module that only provides a default export.
fixUse the default import syntax for ESM: `import Retry from 'promised-retry';`.
This package requires Node.js version 16.0.0 or higher.
Running `promised-retry@0.6.0` or newer on an unsupported Node.js version.
fixUpgrade your Node.js runtime to version 16.0.0 or later. If using `nvm`, run `nvm install 16` and `nvm use 16` (or a newer version).
Audit
Dependencies
No dependency data recorded yet.