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.
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.
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.
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
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.
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.
This gives us an easy way to profile our NodeJS application in production and see some other useful information.
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.
Then if you send a bunch of requests to our example API you can see new messages coming into the log stream.
We also have the following commands to empty all the log files and reload the logs.
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
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.
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!