Keep your Node.js applications online with PM2
In this post, we will guide you through the process of how to keep your Node.js applications online with PM2. This post will cover the very basics. In later posts we will go through some more advanced startup scripts.
It’s happened to the best of us. You’re finally done building your application. The application gets deployed to a server or run locally. After a while, the following message appears uncaughtException
, and the application appears to have stopped.
The logical solution would be to wrap the code that’s breaking inside a try-catch
segment. The big overlooked drawback of this solution is that “ignoring” an uncaughtException
by resuming the program, is the equivalent of pulling the plug on your computer. It goes fine most of the time, but it can also corrupt the system.
The question arises, how would we solve a crashed application more reliably?
Triggering our crash
In order to reliably solve a crashed application, we can implement an external monitor. One such monitor is PM2.
PM2 is a daemon process manager that will help you manage and keep your application online 24/7
For readers who are not all familiar with Unix-like systems, a daemon
is a process that runs unattended in the background. The Windows equivalent of Unix daemons is services
. Thus, PM2 is a “service” that runs in the background of our systems and watches over our Node.js processes.
To better understand the actual benefit of a process manager, let’s look at an example. Create a file somewhere on your system called crash.js
and add the following code:
console.log('application started!');
setTimeout(() => {
throw 'Kaboom!';
}, 5000);
This code starts by printing a console message: application started
. Next, it creates a timeout which will be triggered 5 seconds
after running our script. Inside this timeout, we throw a new exception with the message Kaboom
. This will print the Kaboom
to our console and stop our script’s execution.
Let’s run this script now by executing node crash.js
. After 5 seconds, you will see the following greeting message inside your terminal:
node crash.js
Starting my application
/home/matthias/Documents/Personal/obz/crash.js:4
throw 'Kaboom!';
^
Kaboom!
(Use `node --trace-uncaught ...` to show where the exception was thrown)
Because our app has now crashed, we need to manually start it again, which will require us to monitor it ourselves for when it may crash again. As a direct result, this will cost us our own time and energy.
Keep your Node.js applications online – PM2 to the rescue
Next, let’s try this same script, but this time by using PM2. Install PM2 on your system with npm install pm2 -g
. When it’s done installing, run your crash.js
script again but this time use the following command pm2 start crash.js
. You should see something like the following output:
╰─>$ pm2 start crash.js
[PM2] Applying action restartProcessId on app [crash](ids: [ 0 ])
[PM2] [crash](0) ✓
[PM2] Process successfully started
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ crash │ default │ N/A │ fork │ 344862 │ 0s │ 4 │ online │ 0% │ 18.0mb │ matthias │ disabled │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
You might be wondering at this point where your output has gone and, more importantly, how to see whether your script crashed. For this, we use pm2 monit
. Upon executing this, you should see a monitor screen appear.

In the top left panel, you will find your script name. On the right panel, it shows the crash logs, which in our case, are,
crash > Kaboom!
crash > Starting my application
crash > Kaboom!
crash > Starting my application
In this screen, the green colors are regular messages that have nothing to do with the crash, and the red colors are crash messages. The crash triggers the red messages.
We see now that every time our script crashes, PM2 is there to boot it up again immediately!