
Node.js is an open-source JavaScript runtime that allows you to run JavaScript code outside the browser. Built on Chrome's V8 engine, it is known for its speed and scalability, making it ideal for real-time and high-performance applications. Running Node applications in production faces unique challenges — you need to handle unexpected crashes, manage processes, and ensure continuous uptime. PM2 is a Production Process Manager for Node.js applications that simplifies application management, allowing you to monitor, restart, and optimize your applications.
This article explains how to manage Node Applications with PM2, explore its essential features, and follow best practices for managing your Node applications in a production environment.
Install PM2 via the NPM package manager.
Follow the steps below to create an example Node.js application.
Navigate to the user's home directory.
Create a file named app.js using a text editor such as vim.
Add the following code to the file.
Save and exit the file.
The above app.js file is an example Node.js web application that listens on port 3000 and when it receives an HTTP request, it sends back a response with message "Hello World".
Start your Node.js application with PM2. Replace app.js with your application's entry file if needed.
Your output should be similar to the one below:
Below are some additional parameters to manage your application:
--name <app_name> - specify an application name--watch - restart your application automatically when a file changes--max-memory-restart <memory> - a memory limit that triggers an automatic restart when reached--log <log_file> - specify a log file--restart-delay <delay> - delay between automatic restarts in milliseconds--time - prefix logs with time--no-autorestart - turn off automatic application restarts--cron - a cron job pattern for automatic application restarts--no-daemon - do not run the application in a daemon processVisit the official documentation to view all available parameters.
Manage your Node.js application using PM2 and perform actions like restart, reload, stop and delete PM2 processes.
Restart your PM2-managed application.
Reload your PM2-managed application.
Stop your PM2-managed application.
Delete your PM2-managed application.
List all the applications that are managed by PM2.
View the logs produced by your application in real-time.
View older logs, by specifying the number of lines using --lines flag.
Monitor the amount of resources consumed by your applications in near real time.
Configure a web-based PM2 dashboard.
Follow the prompts to create or log in to your PM2 account. If you don’t have an account, complete the required information to create one and accept the license agreement.
You’ll see the following output during the setup process:
Replace vultrexampleuser with your preferred username, admin@example.com with your log in email address and provide a strong password to secure your PM2 account.
Next Steps:
PM2 Plus is a subscription-based offering that incurs a cost, depending on the number of processes you want to monitor. You can review the pricing details on the PM2 Plus page.
Generate a startup script and configure PM2 to start on server boot.
Your output should be similar to the one below:
Copy the command from previous command output and execute it to make the PM2 process persistent.
Your output should be similar to the one below:
Configure your PM2 applications to start automatically on server boot by saving them into an application list.
Save the running applications to the list.
Your output should be similar to the one below:
If you need to restore the saved applications manually from the list, use the below command.
Your output should be similar to the one below:
Print a list of outdated NPM packages on your system.
This command displays a list of globally installed NPM packages that have newer versions available.
Update the PM2 package via NPM. The update stops all your running PM2 processes until upgrade is completed and your applications will face downtime during this period.
Perform an in-memory update of PM2.
Your PM2 processes may restart multiple times during their lifecycle, causing temporary downtime. Use PM2's cluster mode to reduce downtime. Cluster mode runs multiple instances of your application. When a restart is required, only one instance stops and restarts at a time, while other instances continue to serve your application.
Delete the application before switching to cluster mode.
Start the application in cluster mode, specifying the number of instances with the -i parameter.
This command starts three instances of your application in cluster mode using PM2.
Scale the application by specifying the new number of instances.
This command will scale up your application to have 4 instances of app application.
PM2 ensures graceful shutdowns by sending signals to your application so it can terminate cleanly. This process helps prevent data loss or corruption by allowing your app to finish active tasks before exiting.
PM2 handles graceful shutdowns as follows:
SIGINT signal to your application.Add the following code to enable graceful shutdown in your application:
The above code enables graceful shutdown in your application. It intercepts the SIGINT signal, logs a message to the console, closes the HTTP server, and exits the process.
In this article, you learned how to install and use PM2 to manage Node.js applications in a production environment. You configured PM2 to start applications automatically on system boot, monitored resource usage, viewed logs in real-time, and explored web-based dashboard capabilities with PM2 Plus. By following these steps, you can keep your Node.js applications running reliably, recover from crashes automatically, and simplify operational tasks using PM2’s robust feature set. For more information please visit the official documentation.
0 Comments
Be the first to comment and share your perspective with the community.