Sling Academy
Home/Node.js/PM2 commands to manage a Node.js app in production

PM2 commands to manage a Node.js app in production

Last updated: December 30, 2023

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.

Next Article: PM2: How to auto-restart Node.js app on server reboot

Previous Article: How to implement Facebook Sign In with Express.js

Series: Node.js Intermediate Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates