Manage NodeJS Production Processes With PM2

An introduction to managing NodeJS in production using PM2

Matthew Brown
Oct 29 · 5 min read
Image for post
Image for post
Made by the author in Canva

If you manage a NodeJS application in production PM2 can make your life much easier. PM2 is a full-featured process management tool for NodeJS in production environments.

With PM2 you can be sure that your app’s process will automatically restart if it happens to crash. It also provides handy monitoring tools and the ability to load balance across multiple clusters. We will get into this and much more in this article.

Image for post
Image for post
Screenshot by the author

Demo Application

To demonstrate the full power of PM2 we will need an example NodeJS application to manage. Before we get started we will need to create a basic example of a NodeJS and Express application which we will be running with PM2 in the next section.

The first step will be to create a new npm project and install express as a dependency. Run the following commands in a terminal.

mkdir pm2-nodejs-example
cd pm2-nodejs-example/
npm init -y
npm install --save express

After setup is complete we just need to add a few lines of code to a new file. We will name it index.js and add the following to the contents of the file.

This is pretty much the most basic version of a NodeJS/Express application we can build but it is not that important for this demo.

PM2

To start working with PM2 we will first need to install it globally through npm. Run the following command in a terminal to install.

npm install pm2 -g

Now that we have it installed let’s get the process up and going by running the following command.

Note: You may need to add sudo to the command to run as the root user.

pm2 start index.js// add watch option to automatically restart on file changes
pm2 start index.js --watch
// provide a name for the process
pm2 start index.js --name example-api
Image for post
Image for post
Screenshot by the author

We can see from the printout in the terminal that our process is one and see some additional information about the process. If you want to see a list of the current commands you can run this next command.

pm2 list
Image for post
Image for post
Screenshot by the author

You can stop, restart, and remove the process with the following commands and either pass a process id or “all” as an argument.

pm2 stop all
pm2 restart 0
pm2 delete all

Let’s get to the monitoring next. Run the following to bring up the PM2 monitoring view.

pm2 monit
Image for post
Image for post
Screenshot by the author

This gives us an easy way to profile our NodeJS application in production and see some other useful information.

Logs

PM2 also provides a way to view the logs for your application right in your terminal. Run the following command to stream the current logs.

pm2 logs

Then if you send a bunch of requests to our example API you can see new messages coming into the log stream.

Image for post
Image for post
Screenshot by the author

We also have the following commands to empty all the log files and reload the logs.

pm2 flush
pm2 reloadLogs

Cluster Mode

We can run our app in cluster mode. This means PM2 will run multiple processes and load balance requests across those different processes.

Run the following command to run in cluster mode with the maximum number of processes.

Note: If you have any processes still running you will need to remove them all with pm2 delete all

pm2 start index.js -i max
Image for post
Image for post
Screenshot by the author

The max number of processes is determined by the number of cores on the machine you are running on. For example, my laptop has 8 cores so when I run the max clusters I have 16 processes running at the same time. So 2 processes per core.

Now let’s go back and stream the logs again, and then open up a second terminal and run the following command to simulate a bunch of requests coming into our server.

curl -s "http://localhost:3000?[1-1000]"

As these requests run you should be able to see in the logs which process id is responding to requests come in.

If we open up the monitoring view again we can see we have quite a few more options on the menu.

Image for post
Image for post
Screenshot by the author

This is just scratching the surface of what PM2 can do but is a good primer for getting started with PM2 for managing your production NodeJS processes. Thank you for reading!

The Startup

Medium's largest active publication, followed by +734K people. Follow to join our community.

Matthew Brown

Written by

I am a software architect and lead developer. My passions include software development, anything technology related, and cars.

The Startup

Medium's largest active publication, followed by +734K people. Follow to join our community.

Matthew Brown

Written by

I am a software architect and lead developer. My passions include software development, anything technology related, and cars.

The Startup

Medium's largest active publication, followed by +734K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store