PM2 commands to manage a Node.js app in production

Updated: December 30, 2023 By: Guest Contributor Post a comment

Introduction

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser. In many production environments, deploying and managing a Node.js application can be challenging due to the need for efficient process management, seamless updates, and maximized uptime. PM2 (Process Manager 2) is a popular process management tool that helps in handling these needs. In this tutorial, we’ll explore various PM2 commands that are essential for managing a Node.js application in production. We’ll start with basic commands for process management and graduality move to more advanced monitoring and deployment techniques.

Getting Started with PM2

npm install pm2 -g

After installation, you can start your Node.js app with PM2:

pm2 start app.js

Basic Commands

Start your application with:

pm2 start app.js --name="my-app"

List all running processes:

pm2 list

Stop an application with:

pm2 stop my-app

Restart an Application:

pm2 restart my-app

Delete an application from PM2’s registry:

pm2 delete my-app

Logs and Monitoring

Show all logs for all processes:

pm2 logs

Show logs for a specific process:

pm2 logs my-app

Real-time monitoring:

pm2 monit

Advanced Features

Cluster Mode

To take full advantage of multi-core systems, you can start your application in cluster mode:

pm2 start app.js -i max

Environment Management

Managing environment variables inline:

pm2 start app.js --env production

…or through ecosystem.config.js:

module.exports = {
  apps : [{
    name: 'my-api',
    script: 'app.js',
    env: {
      NODE_ENV: "development",
    },
    env_production : {
       NODE_ENV: "production"
    }
  }]
};

To start in production mode, use:

pm2 start ecosystem.config.js --env production

Deployment with PM2

Setting up the deployment configuration:

module.exports = {
  deploy : {
    production : {
      user : 'node',
      host : '212.83.163.1',
      ref  : 'origin/master',
      repo : '[email protected]:repo.git',
      path : '/var/www/production',
      'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
    },
  }
};

Executing deployment with:

pm2 deploy production setup
pm2 deploy production

Saving and Restoring Process State

It is crucial to ensure process states persist across machine restarts. Here’s how to save the current state:

pm2 save

To resurrect the saved processes:

pm2 resurrect

Updating PM2

To update PM2 to the latest version:

npm install pm2@latest -g
pm2 update

Conclusion

This tutorial introduced you to PM2 commands for managing a Node.js application in production, with a focus on efficiency and robustness. We covered the essentials as well as some advanced techniques such as deployment and process state management. The ultimate goal of mastering PM2 is to provide a scalable, high-availability environment for your Node.js applications in product.