Guide To Setup A Node.js Production Environment Using PM2

Furqan Aziz
2 min readDec 21, 2023

Introduction

Node.js, a robust back-end JavaScript runtime, is pivotal for web and mobile app development. Achieving a seamless production deployment of Node.js is made effortless with the PM2 process manager, ensuring optimal performance and scalability, even in large-scale environments.

PM2, renowned for features like automatic restarts and load balancing, enhances the reliability of your Node.js applications. This article provides a comprehensive guide to setting up Node.js production with PM2, simplifying management for sustained, efficient operation with precision and ease.

Understanding Node.js Production Environment

By default, Node.js runs in a development environment. Both environments are based on different configurations, such as logging, error handling, database connections, etc. Which is controlled by setting the NODE_ENV=production environment variable.

PM2 Introduction

PM2 is the process manager mainly used to manage and deploy Node.js applications to handle different tasks like start, stop, restart, etc., and keep applications running with minimal downtime.

Pre-Requisites

Before starting, make sure that you have the latest version of Node.js and npm installed on your machine. If not, you can install using the NVM node version manager, a tool that is used to manage node versions on your machine. easy to install both at the same time.

Steps to Install Node & NPM using NVM

To install or update nvm, you can use the following cURL or Wget command:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Usage & Verification

You should restart the shell or run the following commands based on which shell is being used.

bash: source ~/.bashrc
zsh: source ~/.zshrcksh: . ~/.profile

Run the following command to verify the installation.

command -v nvm

Node Installation

To list available versions use the following command.

nvm ls-remote

To install the latest version use the following, by default it installs the latest version of Node.js

nvm install node or <specify the required version>

Now, you are ready to install the PM2 on your machine.

For more in-depth insights and tips, check out our detailed blog post.👉https://invozone.com/blog/guide-to-setup-node-js-production-environment/

--

--