Managing and Deploying Node.js Applications with PM2

Node.js is an open-source, cross-platform JavaScript run-time environment for executing JavaScript code on server-side, and uses the Chrome V8 JavaScript engine. Node.js enables JavaScript to be used for server-side scripting and runs scripts on server-side to produce dynamic web page content before the page is sent to the user’s web browser.

We always use our CLI to run and debug our Node.js program, Application will run as long as our program executed on CLI. If we have any error occurred on Node.js program on run-time, it will stop the entire application. So, We need a process management tool to run our Node.js application as a process to run on background and to start automatically if any errors occurs.

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

Deploying Node.js Applications with PM2

Created a sample PM2 application, Which will run our Node.js program on background and record the error and output of the program in a log file. We can manage our application up-time, memory and lot more using PM2, Check on the PM2 documentation for more usage commands.

{
  "name": "demo",
  "script": "./app.js",
  "watch": false,
  "error_file": "logs/error.log",
  "out_file": "logs/out.log",
  "env": {
    "NODE_ENV": "development"
  },
  "env_production": {
    "NODE_ENV": "production"
  }
}

Download/Demo:

Download

Please leave your valuable comments/suggestions/feedback on below comment box if any.

Thank You !!

Leave a Reply

Your email address will not be published. Required fields are marked *