How to use PM2 for Node Js Applications

How to use PM2 for Node Js Applications

If you’ve worked on any front-end applications recently, you have likely had some contact with a Node/Express application. Have you ever thought about how Node.js applications are deployed and run on the server? We all must have used nodemon for application development but we can not use the same for production environments.

 

I recently had a project where I needed to set up a Node application in a production environment. I needed a tool to solve the technical challenges of managing the Node processes and deploying the application in a very efficient manner. I chose PM2 for node js, which is a process manager that allows you to keep your applications alive.

 

In this article, we will see what is PM2 and where we use it in node.js application development. 

 

PM2 or Process Manager 2 is an incredibly versatile production process manager written in Node.js. It is a daemon process manager that will help you manage and keep your application online.

 

Before digging into PM2, PM2 logs, PM2 monitoring or PM2 commands, you might want to read up on the benefits of using it.

 

Benefits of Using PM2

PM2 for node js enables you to keep applications alive forever. Besides this, it also helps reload applications without downtime, manage application logging, monitoring, and clustering.

 

Here are the core features

  • Process management including automatic app restarts on failure or system reboots
  • Application monitoring
  • Declarative configuration via JSON file
  • PM2 Logs management
  • Built-in cluster mode
  • Startup script generation for *nix systems
  • Seamless updates
  • Integrated module system

 

Installing PM2

To look at how to conduct PM2 install, let’s dive in. 

Firstly, PM2 should be installed globally to run several applications.

 

npm install pm2 -g

 

PM2 uses a configuration file to maintain the application. It can be either JSON or js or a YAML file.

 

Here, we are going to use the process.json file which will have the configuration for our application.

It contains the apps array which contains the application configuration details

{

    "apps": [{

        "name" : "nodepm2",

        "script" : "./app.js",

        "env" : {

            "PORT" : 4005

        },

        "env_production" : {

            "PORT" : 4000

        }

    }]

}

 

Firstly, name is the name of the process that PM2 is running. PM2 will run the file mentioned in the script.  Here, it will run like

 

pm2 start app.js

 

env contains the environment variable for the application. We can specify different env variables such as development, production, or staging.

 

Here, we mentioned the environment variables for production and default(development).

 

Running PM2

To conduct a PM2 start or run the application using PM2, we can start the application using the configuration of pm2.

 

pm2 start process.json

 

We can stop the node application process by specifying the process id.

 

pm2 stop 0


 

To delete the process once it is stopped, specify the process id with the delete command.

 

pm2 delete 0

 

If we want to get rid of all the processes then do

 

pm2 delete all 

 

We can run the PM2 with different environment variables set up by specifying the env in the PM2 command.

 

pm2 start process.json --env production

 

For listing all the process that pm2 running

 

pm2 list

 

The only difference between reload and restart is the reload will restart the process(es) with zero downtime.

 

pm2 restart 0

pm2 reload 0

 

For more information, you can refer to this: https://www.npmjs.com/package/pm2


If you still need any information about PM2 startup, PM2 restart, PM2 Node, or any other aspect related to PM2 for node js, get in touch with our experts!

Shreya Sangaonkar
Shreya Sangaonkar
Tech Lead
7 Rules to Follow and Stay Motivated at Work

7 Rules to Follow and Stay Motivated at Work

Arisen Fernandes
Journey From Seminar To Webinar

Journey From Seminar To Webinar

Sanjeet Kanekar
Banner

Add and Remove Form fields dynamically with React and React Hooks

Sadam Bapunawar