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.

pm2 features

Installing pm2

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

1npm 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

1npm init --yes

add the following code in app.js.

1const express = require("express")
2
3const app = express()
4
5app.get("/", (req, res) => {
6 res.send("PM2 is working, Send me Home")
7})
8
9const PORT = process.env.PORT
10app.listen(PORT, () => {
11 console.log(`server is running on port ${PORT}`)
12})

After that, create a file called process.json. it contains the apps array which contains the application configuration details.

1{
2 "apps": [{
3 "name" : "nodepm2",
4 "script" : "./app.js",
5 "env" : {
6 "PORT" : 4005
7 },
8 "env_production" : {
9 "PORT" : 4000
10 }
11 }]
12}

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

1pm2 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.

1pm2 start process.json

pm2 dev

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

1pm2 stop 0

stop command

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

delete command

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

1pm2 start process.json --env production

prod command 1

For listing all the process that pm2 running,

list command

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

Author

Hey, I’m Ganesh, Full stack engineer.I love to write technical content and help developers like me to grow in the industry. please consider supporting me.

To Read More

Building a Production-grade Nodejs,...

This article is the first part of building a production grade nodejs,graphql and...

Modern React Redux Tutorials with R...

This tutorial explain how you can build an application using modern react redux ...

Building a Piano with React Hooks

In this article, we will see how to build a piano with react hooks. Building a P...

TypeScript Basics - The Definitive ...

In this article, we will learn some basics of typescript which helps you to deve...

Here's why podman is more secured t...

In this article we will see about podman and why it is more secured way to run c...

Building a Production - Ready Node....

In this article, we will see how to build a Nodejs, TypeScript Application and d...

Nginx for Front-end Developers

This article is to explain Nginx for Front-end Developers in a much simpler way....

What is gRPC ? How to implement gRP...

Everyone talks about gRPC. Have you ever wonder how it works or how to implement...