0

PM2 for Node.js developers

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

Here are recent articles about Node.js,

Building P2P video chat App using Node.js

Understanding EventEmitters in Node.js

Apache Kafka for Node.js developers

Have you ever thought about how Node.js applications are deployed and run in the server?.

As we all know, we use Nodemon for application development. But, can’t we use the same in the production server.

Actually, there are some problems with that. While using nodemon, if you close the terminal, it will stop the process of the node.js app. that will a problem in Production.

we want the application to run without any interruption in the Production server.

there are few npm packages out there that solve this problem. one of the popular npm packages is pm2.

what is pm2?

pm2 is a process manager in production for Node.js applications. it helps to manage the processes of Node.js applications. it comes with a lot of advanced feature such as application cluster, load balancing, etc.

Installing pm2

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 YAML file.

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

Application Setup

Create a simple node.js application with a file called app.js

npm init --yes

add the following code in app.js.

const express = require('express');

const app = express();

app.get('/',(req,res) => {

    res.send("PM2 is working, Send me Home");
    
})

const PORT = process.env.PORT;
app.listen(PORT,() => {
    console.log(`server is running on port ${PORT}`);
})

After that, create a file called process.json. 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 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

Therefore, To 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.

we can run the pm2 with different environment variables set up by specifying the env in pm2 command.

pm2 start process.json --env production

For listing all the process that pm2 running,

Summary

pm2 is one of the most used libraries for managing the node.js application process in the production server.

There are a lot of advanced features in pm2 that will be covered in upcoming articles.

Until then, Happy Coding

ganeshmani009

Leave a Reply

Your e-mail address will not be published. Required fields are marked *