Production process manager for Node.JS applications with a built-in load balancer.
npx @tessl/cli install tessl/npm-pm2@6.0.0PM2 is a production process manager for Node.js applications with a built-in load balancer. It provides daemon-like process management, built-in load balancing, zero-downtime reloads, cluster mode for horizontal scaling, comprehensive monitoring, automatic crash recovery, and container integration. PM2 is essential for production Node.js application lifecycle management with enterprise-grade reliability and performance optimization capabilities.
npm install pm2 -g (global) or npm install pm2 (local)pm2, pm2-dev, pm2-docker, pm2-runtime commandsFor programmatic use:
const pm2 = require('pm2');For TypeScript:
import * as pm2 from 'pm2';
import { StartOptions, ProcessDescription } from 'pm2';Custom PM2 instance:
const PM2 = require('pm2').custom;
const customPm2 = new PM2({
pm2_home: '/custom/path',
daemon_mode: false
});const pm2 = require('pm2');
// Connect to PM2 daemon
pm2.connect((err) => {
if (err) {
console.error(err);
process.exit(2);
}
// Start a process
pm2.start({
script: 'app.js',
name: 'my-app',
instances: 'max',
exec_mode: 'cluster'
}, (err, apps) => {
if (err) {
console.error(err);
return pm2.disconnect();
}
// List all processes
pm2.list((err, processes) => {
console.log(processes);
pm2.disconnect();
});
});
});# Start an application
pm2 start app.js --name "my-app" -i max
# List all processes
pm2 list
# Monitor processes
pm2 monit
# Stop all processes
pm2 stop all
# Reload with zero downtime
pm2 reload all
# Save current process list
pm2 save
# Resurrect saved processes
pm2 resurrectPM2 is built around several key components:
Core process lifecycle operations including starting, stopping, restarting, and deleting managed processes. Supports multiple execution modes and clustering.
// Connection management
function connect(callback: (err: Error) => void): void;
function connect(noDaemonMode: boolean, callback: (err: Error) => void): void;
function disconnect(): void;
// Process lifecycle
function start(options: StartOptions, callback: (err: Error, proc: Proc) => void): void;
function stop(process: string | number, callback: (err: Error, proc: Proc) => void): void;
function restart(process: string | number, callback: (err: Error, proc: Proc) => void): void;
function reload(process: string | number, callback: (err: Error, proc: Proc) => void): void;
function delete(process: string | number, callback: (err: Error, proc: Proc) => void): void;Process monitoring, system metrics collection, and detailed process information retrieval. Includes real-time monitoring interfaces and performance profiling.
function list(callback: (err: Error, processes: ProcessDescription[]) => void): void;
function describe(process: string | number, callback: (err: Error, description: ProcessDescription[]) => void): void;
function launchSysMonitoring(callback?: (err: Error) => void): void;
function profile(type: 'cpu' | 'mem', time?: number, callback?: (err: Error) => void): void;PM2 configuration management including getting, setting, and persisting configuration values. Supports both runtime and persistent configuration.
function get(key?: string, callback?: (err: Error) => void): void;
function set(key: string, value: any, callback?: (err: Error) => void): void;
function multiset(values: string, callback?: (err: Error) => void): void;
function unset(key: string, callback?: (err: Error) => void): void;Comprehensive command-line interface providing 68 commands for process management, monitoring, configuration, and system integration.
# Core process management
pm2 start <script|config> [options]
pm2 stop <id|name|all>
pm2 restart <id|name|all>
pm2 reload <id|name|all>
pm2 delete <id|name|all>
# Information and monitoring
pm2 list
pm2 describe <id|name>
pm2 monitPM2 module system for extending functionality with plugins and add-ons. Install, develop, package, and publish PM2 modules for production enhancements.
function install(module_name: string, options?: InstallOptions, callback?: (err: Error) => void): void;
function uninstall(module_name: string, callback?: (err: Error) => void): void;
function generateModuleSample(app_name: string, callback?: (err: Error) => void): void;
function package(module_path: string, callback?: (err: Error) => void): void;
function publish(folder: string, options?: PublishOptions, callback?: (err: Error) => void): void;Git-based deployment and version management for production applications. Automated deployment, rollback, and source code management.
function pullAndRestart(process_name: string, callback?: (err: Error, result: any) => void): void;
function pullAndReload(process_name: string, callback?: (err: Error, result: any) => void): void;
function pullCommitId(process_name: string, commit_id: string, callback?: (err: Error, result: any) => void): void;
function backward(process_name: string, callback?: (err: Error, result: any) => void): void;
function forward(process_name: string, callback?: (err: Error, result: any) => void): void;Container-optimized runtime modes, Dockerfile generation, and production-ready containerization with proper signal handling.
function generateDockerfile(script: string, options?: DockerOptions): string;
function dockerMode(script: string, options?: DockerOptions, mode?: string): any;pm2-runtime <script|config> [options]
pm2-docker <script|config> [options]interface StartOptions {
name?: string;
script?: string;
args?: string | string[];
cwd?: string;
env?: { [key: string]: string };
instances?: number;
exec_mode?: string;
watch?: boolean | string[];
merge_logs?: boolean;
output?: string;
error?: string;
log_date_format?: string;
autorestart?: boolean;
max_restarts?: number;
max_memory_restart?: number | string;
kill_timeout?: number;
restart_delay?: number;
wait_ready?: boolean;
namespace?: string;
}
interface ProcessDescription {
name?: string;
pid?: number;
pm_id?: number;
monit?: {
memory?: number;
cpu?: number;
};
pm2_env?: {
pm_cwd?: string;
pm_out_log_path?: string;
pm_err_log_path?: string;
status?: ProcessStatus;
instances?: number | 'max';
pm_uptime?: number;
restart_time?: number;
unstable_restarts?: number;
};
}
type ProcessStatus = 'online' | 'stopping' | 'stopped' | 'launching' | 'errored' | 'one-launch-status' | 'waiting_restart';