A Node.js process manager is a useful tool to ensure that a Node.js process or script runs continuously (forever) and can enable it to auto-start at system boot.
It allows you to monitor the running services and it facilitates common system administration tasks (such as restarting on failure, stopping, reloading configurations without downtime, modify environment variables/settings, showing performance metrics and so much more). It also supports application logging, clustering, and load balancing, and so many other useful process management features.
Read Also: 14 Best NodeJS Frameworks for Developers in 2019
A package manager is useful especially for deployment of Node.js applications in a production environment. In this article, we will review four process managers for Node.js application management in a Linux system.
1. PM2
PM2 is an open-source, advanced, feature-rich, cross-platform and the most popular production-level process manager for Node.js with a built-in load balancer. It allows you to list, monitor and act on all launched Nodejs processes, and it supports cluster mode.
It supports application monitoring: offers a simple way to monitor the resource (memory and CPU) usage of your application. It supports your process management workflow by allowing you to configure and tune the behavior of each application via a process file (supported formats include Javascript, JSON, and YAML).
Application logs are always key in a production environment, in this regard PM2 allows you to easily manage your application’s logs. It provides different ways and formats for handling and displaying logs respectively. You can display logs in real-time, flush them, and reload them when needed.
Importantly, PM2 supports startup scripts which you can configure to auto-start your processes across expected or unexpected machine restarts. It also supports auto-restart of an application when a file is modified in the current directory or its sub-directories.
In addition, PM2 comes with a module system which allows users to create custom modules for Nodejs process management. For example, you can create a module for log rotation module or load balancing, and so much more.
Last but not least, if you are using Docker containers, PM2 allows for container integration, and offers an API system that allows you to use it programmatically.
2. StrongLoop PM
StrongLoop PM is also an open-source, advanced production process manager for Node.js applications with built-in load balancing just like PM2 and it can be used via a command-line or a graphical interface.
It supports application monitoring (view performance metrics such as event loop times, CPU and memory consumption), multi-host deployment, cluster mode, zero-downtime application restarts and upgrades, automatic process restart on failure, and log aggregation and management.
Furthermore, it ships with Docker support, allows you to export performance metrics to StatsD-compatible servers, and view in 3rd-party consoles such as DataDog, Graphite, Splunk as well as Syslog and raw log files.
3. Forever
Forever is an open-source, simple and configurable command-line interface tool to run a given script continuously (forever). It is suited for running smaller deployments of Node.js apps and scripts. You can use forever in two ways: through the command-line or by embedding it in your code.
It allows you to manage (start, list, stop, stop all, restart, restart all, etc..) Node.js processes and it supports watching for file changes, debug mode, application logs, killing of a process and exit signal customization, and so much more. In addition, it supports several usage options which you can pass directly from the command line or passe them in a JSON file.
4. SystemD – Service and System Manager
In Linux, Systemd is a daemon that manages system resources such as processes and other components of the file system. Any resource managed by systemd is known as a unit. There are different types of units including service, device, socket, mount, target and many other units.
Systemd manages units via a configuration file known as a unit file. Therefore, in order to manage your Node.js server like any other system services, you need to create for it a unit file, which in this case will be a service file.
Once you have a created a service file for your Node.js server, you can start it, enable it to auto-start at system boot time, check its status, restart (stop and start it again) or reload its configuration, and even stop it like any other systemd services.
For more information, see: How to Create and Run New Service Units in Systemd Using Shell Script
Summary
A Node.js package manager is a useful tool for deploying your project in a production environment. It keeps an application alive forever and simplifies how you can control it. In this article, we reviewed four package managers for Node.js. If you have any additions or questions to ask, make use of the feedback form below to reach us.