PM2 for Node.js developers
Tags: showdev, webdev, tutorial, beginners
Published: 2019-09-15 07:48:49

---
title: PM2 for Node.js developers
published: true
description: PM2 for Node.js developers
tags: showdev,webdev,tutorial,beginners
canonical_url: https://cloudnweb.dev/2019/09/pm2-for-node-js-developers/
---
<!-- wp:paragraph -->
<p>In this article, we will see what is pm2 and where we use it in node.js application development. PM2 for Node.js developers</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here are recent articles about Node.js,</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/09/building-p2p-video-chat-application-using-webrtc-and-node-js/">Building P2P video chat App using Node.js</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/08/understanding-eventemitter-in-node-js-with-a-usecase/">Understanding EventEmitters in Node.js</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/08/apache-kafka-for-node-js-developers/">Apache Kafka for Node.js developers</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Have you ever thought about how Node.js applications are deployed and run in the server?.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As we all know, we use Nodemon for application development. But, can't we use the same in the production server.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Actually, there are some problems with that. While using nodemon, if you close the terminal, it will stop the process of the node.js app. that will a problem in Production.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>we want the application to run without any interruption in the Production server.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>there are few npm packages out there that solve this problem. one of the popular npm packages is <strong>pm2</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>what is pm2?</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p> pm2 is a process manager in production for Node.js applications. it helps to manage the processes of Node.js applications. it comes with a lot of advanced feature such as application cluster, load balancing, etc.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1340} -->
<figure class="wp-block-image"><img src="https://cloudnweb.dev/wp-content/uploads/2019/09/pm2_features-1024x315.png" alt="" class="wp-image-1340"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Installing pm2</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Firstly, pm2 should be installed globally to run several applications.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>npm install pm2 -g</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p> pm2 uses a configuration file to maintain the application.it can be either JSON or js or YAML file.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here, we are going to use <strong>process.json</strong> file which will have the configuration for our application.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Application Setup</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Create a simple node.js application with a file called <strong>app.js</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>npm init --yes</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>add the following code in app.js.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>const express = require('express');

const app = express();

app.get('/',(req,res) => {

    res.send("PM2 is working, Send me Home");
    
})

const PORT = process.env.PORT;
app.listen(PORT,() => {
    console.log(`server is running on port ${PORT}`);
})</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>After that, create a file called <strong>process.json</strong>. it contains the apps array which contains the application configuration details.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>{
    "apps": [{
        "name" : "nodepm2",
        "script" : "./app.js",
        "env" : {
            "PORT" : 4005
        },
        "env_production" : {
            "PORT" : 4000
        }
    }]
}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Firstly, <strong>name </strong>is the name of the process that pm2  running. pm2 will run the file mentioned in the script. Here, it will run like</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>pm2 start app.js</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>env</strong> contains the environment variable for the application. we can specify different env variables such as development, production or staging. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here, we mentioned the environment variables for production and default(development).</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Running pm2</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p> Therefore, To run the application using pm2, we can start the application using the configuration of pm2.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>pm2 start process.json</code></pre>
<!-- /wp:code -->

<!-- wp:image {"id":1343} -->
<figure class="wp-block-image"><img src="https://cloudnweb.dev/wp-content/uploads/2019/09/pm2_dev.png" alt="" class="wp-image-1343"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>we can stop the node application process by specifying the process id.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>pm2 stop 0</code></pre>
<!-- /wp:code -->

<!-- wp:image {"id":1344} -->
<figure class="wp-block-image"><img src="https://cloudnweb.dev/wp-content/uploads/2019/09/stop_command.png" alt="" class="wp-image-1344"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>To delete the process once it is stopped, specify the process id with the delete command.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1345} -->
<figure class="wp-block-image"><img src="https://cloudnweb.dev/wp-content/uploads/2019/09/delete_command.png" alt="" class="wp-image-1345"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>we can run the pm2 with different environment variables set up by specifying the env in pm2 command.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>pm2 start process.json --env production</code></pre>
<!-- /wp:code -->

<!-- wp:image {"id":1346} -->
<figure class="wp-block-image"><img src="https://cloudnweb.dev/wp-content/uploads/2019/09/prod_command-1.png" alt="" class="wp-image-1346"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>For listing all the process that pm2 running,</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1347} -->
<figure class="wp-block-image"><img src="https://cloudnweb.dev/wp-content/uploads/2019/09/list_command.png" alt="" class="wp-image-1347"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Summary</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>pm2 is one of the most used libraries for managing the node.js application process in the production server.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are a lot of <a href="http://pm2.keymetrics.io/docs/usage/quick-start/">advanced features</a> in pm2 that will be convered in upcoming articles. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Until then, Happy Coding</p>
<!-- /wp:paragraph -->