How to use PM2 for Node Js Applications

How to use PM2 for Node Js Applications

If you’ve worked on any front-end applications recently, you have likely had some contact with a Node/Express application. Have you ever thought about how to setup PM2 for Node.js? Or, how Node.js applications are deployed and run on the server? We all must have used nodemon for application development but we can not use the same for production environments.

I recently had a project where I needed to set up a Node application in a production environment. I needed a tool to solve the technical challenges of managing the Node processes and deploying the application in a very efficient manner. I chose PM2 for node js, which is a process manager that allows you to keep your applications alive.

 

In this article, we will see

 

What is PM2

PM2 or Process Manager 2 is an incredibly versatile production process manager written in Node.js. It is a daemon process manager that will help you manage and keep your application online with commands like 'node js PM2 start'.

Before digging into PM2, PM2 logs, PM2 monitoring or PM2 commands, you might want to read up on the benefits of using it.

 

Benefits of Using PM2

PM2 for Node.js enables you to keep applications alive forever. Besides this, it also helps reload PM2 Node.js applications without downtime, manage application logging, monitoring, and configuring PM2 for Node.js load balancing and clustering.

 

Here are the core features

  • Process management including automatic app restarts on failure or system reboots
  • Managing environment variables in Node.js with PM2
  • Application monitoring
  • Declarative configuration via JSON file
  • PM2 Logs management
  • Built-in cluster mode
  • Startup script generation for *nix systems
  • Seamless updates
  • Integrated module system

 

Installing PM2

To look at how to conduct PM2 install, let’s dive in. 

Firstly, PM2 should be installed globally to run several applications.

 

npm install pm2 -g

 

PM2 uses a configuration file to maintain the application. It can be either JSON or js or a YAML file

 

Here, we are going to use the process.json file which will have the configuration for our application.

It contains the apps array which contains the application configuration details

 

{

    "apps": [{

        "name" : "nodepm2",

        "script" : "./app.js",

        "env" : {

            "PORT" : 4005

        },

        "env_production" : {

            "PORT" : 4000

        }

    }]

}

 

Firstly, name is the name of the process that PM2 is running. PM2 will run the file mentioned in the script.  Here, it will run like:

 

pm2 start app.js

 

env contains the environment variable for the application. We can specify different env variables such as development, production, or staging.

Here, we mentioned the environment variables for production and default(development).

 

Running PM2

To conduct a PM2 start or run the application using PM2, we can start the application using the configuration of pm2.

 

pm2 start process.json

 

We can stop the node application process by specifying the process id.

 

pm2 stop 0

 

To delete the process once it is stopped, specify the process id with the delete command.

 

pm2 delete 0

 

If we want to get rid of all the processes then do

 

pm2 delete all 

 

We can run the PM2 with different environment variables set up by specifying the env in the PM2 command.

 

pm2 start process.json --env production

 

For listing all the process that pm2 running

 

pm2 list

 

The only difference between reload and restart is the reload will restart the process(es) with zero downtime.

 

pm2 restart 0

pm2 reload 0

 

For more information, you can refer to this: https://www.npmjs.com/package/pm2

 

FAQs 

 

1. How to run Next.js app with PM2? 

Running a Next.js application with PM2 is a straightforward process that ensures your app remains active. To achieve this, first, build your Next.js app. Then, use PM2's command-line interface to start the app with a command like pm2 start npm --name "next-app" -- run start, ensuring your Next.js app is always running and resilient to crashes.

 

2. How to run npm scripts with PM2?

To execute NPM scripts using PM2, you'll utilize the npm start command within PM2's ecosystem. For instance, pm2 start npm -- start will run the 'start' script defined in your package.json file. This allows you to manage and keep your Node.js applications running seamlessly with PM2 and npm.

 

3. What is the difference between PM2 and cluster in Nodejs?

PM2 and Node.js cluster both aim to optimize app performance and load management. The core difference lies in their approach: Node.js cluster is a module that allows you to create child processes (workers) that share server ports; its primary function is to enable the load balancing feature over multiple CPU cores. PM2, on the other hand, not only provides a load-balancing solution but also adds features like process management, monitoring, and zero-downtime reloads, offering a more comprehensive toolkit for Node.js application sustainability.

 

If you still need any information about PM2 startup, PM2 restart, PM2 Node, how to set up PM2 for Node.js on AWS EC2, or any other aspect related to PM2 for Node.js, get in touch with our experts!

Author Profile Picture
Shreya Sangaonkar
<p>{<br /> &nbsp; "@context": "https://schema.org",<br /> &nbsp; "@type": "FAQPage",<br /> &nbsp; "mainEntity": [<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; "@type": "Question",<br /> &nbsp; &nbsp; &nbsp; "name": "How to run Next.js app with PM2?",<br /> &nbsp; &nbsp; &nbsp; "acceptedAnswer": {<br /> &nbsp; &nbsp; &nbsp; &nbsp; "@type": "Answer",<br /> &nbsp; &nbsp; &nbsp; &nbsp; "text": "Running a Next.js application with PM2 is a straightforward process that ensures your app remains active. To achieve this, first, build your Next.js app. Then, use PM2's command-line interface to start the app with a command like pm2 start npm --name next-app -- run start, ensuring your Next.js app is always running and resilient to crashes."<br /> &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; },<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; "@type": "Question",<br /> &nbsp; &nbsp; &nbsp; "name": "How to run npm scripts with PM2?",<br /> &nbsp; &nbsp; &nbsp; "acceptedAnswer": {<br /> &nbsp; &nbsp; &nbsp; &nbsp; "@type": "Answer",<br /> &nbsp; &nbsp; &nbsp; &nbsp; "text": "To execute NPM scripts using PM2, you'll utilize the npm start command within PM2's ecosystem. For instance, pm2 start npm -- start will run the 'start' script defined in your package.json file. This allows you to manage and keep your Node.js applications running seamlessly with PM2 and npm."<br /> &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; },<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; "@type": "Question",<br /> &nbsp; &nbsp; &nbsp; "name": "What is the difference between PM2 and cluster in Nodejs?",<br /> &nbsp; &nbsp; &nbsp; "acceptedAnswer": {<br /> &nbsp; &nbsp; &nbsp; &nbsp; "@type": "Answer",<br /> &nbsp; &nbsp; &nbsp; &nbsp; "text": "PM2 and Node.js cluster both aim to optimize app performance and load management. The core difference lies in their approach: Node.js cluster is a module that allows you to create child processes (workers) that share server ports; its primary function is to enable the load balancing feature over multiple CPU cores. PM2, on the other hand, not only provides a load-balancing solution but also adds features like process management, monitoring, and zero-downtime reloads, offering a more comprehensive toolkit for Node.js application sustainability."<br /> &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; }<br /> &nbsp; ]<br /> }</p> <p>&nbsp;</p> <p>&lt;script type="application/ld+json"&gt;<br /> {<br /> &nbsp; "@context": "https://schema.org",<br /> &nbsp; "@type": "BlogPosting",<br /> &nbsp; "mainEntityOfPage": {<br /> &nbsp; &nbsp; "@type": "WebPage",<br /> &nbsp; &nbsp; "@id": "https://sjinnovation.com/how-to-setup-pm2-for-nodejs"<br /> &nbsp; },<br /> &nbsp; "headline": "Setup PM2 for Node.js Applications: A Comprehensive Guide",<br /> &nbsp; "description": "Explore the benefits of using PM2, learn how to setup PM2 for Node.js applications, and efficiently manage your Node.js applications in production environments.",<br /> &nbsp; "image": "https://sjinnovation.com/sites/default/files/pictures/blog-post/How%20to%20use%20PM2%20for%20Node%20Js%20Applications.png", &nbsp;<br /> &nbsp; "author": {<br /> &nbsp; &nbsp; "@type": "Person",<br /> &nbsp; &nbsp; "name": "Shreya Salgaonkar"<br /> &nbsp; }, &nbsp;<br /> &nbsp; "publisher": {<br /> &nbsp; &nbsp; "@type": "Organization",<br /> &nbsp; &nbsp; "name": "SJ Innovation LLC",<br /> &nbsp; &nbsp; "logo": {<br /> &nbsp; &nbsp; &nbsp; "@type": "ImageObject",<br /> &nbsp; &nbsp; &nbsp; "url": "https://sjinnovation.com/themes/custom/sji_theme/logo-sji-ai.png"<br /> &nbsp; &nbsp; }<br /> &nbsp; },<br /> &nbsp; "datePublished": "2022-07-18",<br /> &nbsp; "dateModified": "2024-01-29"<br /> }<br /> &lt;/script&gt;</p>
DRUPAL: The CMS you must be looking for!

DRUPAL: The CMS you must be looking for!

Sanjeet Kanekar
Culture &Happiness in SJ Innovation

Culture & happiness in SJI

Madhav Ranganekar
Case Study: TalentResources

Case Study: TalentResources

Admin