Node.js, the open-source JavaScript runtime, is a popular choice for building the backend infrastructure of your application by allowing you to run JavaScript outside web browsers. But what if your Node.js application crashes in production? Find out how you can avoid such scenarios in this article.

What Is PM2 and Why Do You Need It?

PM2 is a daemon process manager that enables you to keep Node.js applications alive forever, reload them without downtime, manage application logging, monitoring, and clustering.

Imagine your Node.js web server crashes due to some runtime error. Without using something like PM2, you would have to manually track the status of your application periodically and restart it whenever it goes down. PM2 helps you automate this process and keeps your application online 24/7, even if things go wrong.

Installing PM2 on Linux Servers

Before you can install PM2, make sure that you have Node.js and NPM installed on your Linux server to download the PM2 package from the Node Package Manager repositories.

Once you get Node.js up and running on your server, install PM2 by running this command:

Output:

The -g flag in the above command stands for Global and allows you to use PM2 across all your Node.js applications. You might need to add sudo at the start of the command, depending on the permissions your user account has.

Running Node Apps With PM2

Once the installation process is completed, navigate to the directory where your Node.js application resides using the cd command. Inside the project directory, you can execute a JavaScript file using PM2 with the command given below:

For example, if the entry point to your Node.js application resides inside the index.js file, this is how you can run the file:

In case you need to make any changes to your application, you can do so using a command-line text editor to edit the code, and then restart your PM2 application instance with this command:

To completely shut down the application instead, use this command:

Instead of running a single file with PM2, you can also execute NPM scripts present inside the package.json file of your Node.js application. For example, if you want to run the start script, structure the command in this manner:

Viewing Logs and Statistics

To monitor the errors that may arise during runtime, PM2 provides you with an option to check out the logs your application generates. Here’s how you can view them:

To monitor all the processes that are currently running, PM2 comes with a handy terminal-based dashboard to observe the CPU and RAM usage, that you can access with this command:

To view the rest of the details and metadata such as the application version, the number of restarts, uptime, script arguments, and more:

Deploy Node.js Apps to Production at Ease

Using a process manager like PM2 can massively reduce the overhead of constantly having to monitor the status and uptime of your Node.js application. With its built-in load balancer, you can expect increased performance and reliability for your services.

Speaking of deployment, you would most probably use a Linux virtual machine to deploy apps to the cloud. Unsure about how you could do that? Here’s a guide to get you started with virtual machines on Azure.