Updated on Oct 5, 2023
PM2 and NPM share letters in their abbreviations. However, they are entirely different managers. While NPM stands for Node Package Manager, PM stands for Process Manager. With PM2, which is the version we will discuss in this tutorial, you can easily manage and monitor your Node.js processes. It does not come with the default Node.js package but is extremely easy to install. Simply use this command: npm install pm2 -g
The command will install PM2 on your system and connect it to your Node.js installation so you can use it immediately. Once it is set up, you can start, stop, or restart any Node.js application. Like NPM, PM2 uses CLI instead of a graphical interface, but the commands are so intuitive you will memorize them instantly. Here are a few of the most important ones:
Here is a quick example how to use PM2, and what it looks like. To start your application with you will need to input the following command via the console.
pm2 start app.js
You will be presented with a table containing all the applications you have added to PM2. In our case, since this is our first app, the table only has one entry. Please note that using the command will give an automatic name to the application which you will have to then use as a reference when executing PM2 commands against the application. For example, to stop the PM2 monitoring over the app.js application get the name of the application using this command.
pm2 ls
That will bring the table with all the applications again. Find the server application which should be currently online. To stop it just run this command.
pm2 stop app
The application will then be listed as stopped.
PM2 is extremely useful if you want to keep your application up and running indefinitely, even if there is a system restart. Typically, if a system is restarted, all running processes are stopped. Then, the ones that are supposed to begin anew when the system comes back online go online themselves. Node.js does not have that functionality by default, which is why PM2 is so useful. It can also show you vital information about your application, how it is running, and if there are any issues with it.