Managing Node.js Process Lifecycles with PM2 | NodeFlux.eu Docs
Use PM2 process manager configuration files to handle auto-restarts, environment injection, and log management.
Steps
- Create ecosystem.config.cjs file in your Node.js project root.
- Define app configuration: module.exports = { apps: [{ name: "my-app", script: "./index.js", maxmemoryrestart: "450M" }] };
- Configure maxmemoryrestart to trigger soft reload if memory leaks push usage near plan RAM caps.
- Set autorestart: true and expbackoffrestart_delay: 100 to prevent tight crash loops.
- Set startup script to pm2-runtime start ecosystem.config.cjs in your service controls.
- Inspect application logs using the service panel output tab.
Notes
- pm2-runtime is designed specifically for container environments to keep PID 1 active.
- Setting memory limits slightly below container RAM quota prevents hard OOM container kills.
Related NodeFlux pages
- NPM Dependency Management: package.json vs package-lock.json — Avoid production deployment crashes by managing NPM dependency trees cleanly with lockfiles.
- Diagnosing and Fixing Node.js Memory Leaks — Identify uncollected object references, event listener leaks, and global state accumulation in JavaScript.
- Setting up WebSocket and Socket.io Real-Time Servers — Host real-time bidirectionally connected apps using Socket.io or native ws module on assigned service ports.