Logo

dev-resources.site

for different kinds of informations.

Guide to Running a Node Server in Nx Monorepo using PM2 Process Manager

Published at
6/25/2023
Categories
nx
pm2
node
Author
jonathandsouza
Categories
3 categories in total
nx
open
pm2
open
node
open
Author
14 person written this
jonathandsouza
open
Guide to Running a Node Server in Nx Monorepo using PM2 Process Manager

Introduction

PM2 is a daemon process manager that will help you manage and keep your application online 24/7.
It has a lot of features that will help you in the process of deploying and maintaining your application.

Additionally PM2 allows you to run a nodeJS server in cluster mode.
Which dramatically improves the performance of your application and allows you to scale your application horizontally.

In this article we will cover the following:

  1. Setting up a Nx monorepo
  2. Setting up a NestJS project within the Nx Monorepo.
  3. Running NestJS with PM2 using Nx-PM2-Plugin.

It must be noted that the same approach can be used for any nodeJS project within Nx.

1. Setting up a Nx Monorepo

In this section we will be setting up a Nx monorepo using the integrated mode scheme.

npx create-nx-workspace@latest myorg --preset=ts
Enter fullscreen mode Exit fullscreen mode

A comprehensive guide to setting up the Nx monorepo can be found here;

2. Setting up NestJS

Nx provides a NestJS schematics to setup a NestJS project within the monorepo.

npm install -D @nx/nest
npx nx g @nx/nest:app my-nest-app
Enter fullscreen mode Exit fullscreen mode

A comprehensive guide to setup NestJS within Nx can be found here

3.Running NestJS with PM2

Install PM2 globally.

npm install pm2 -g
Enter fullscreen mode Exit fullscreen mode

Install Nx-PM2-Plugin

npm i nx-pm2-plugin
Enter fullscreen mode Exit fullscreen mode

Add the following task to project.json of the NestJS project.

"pm2": {
    "executor": "nx-pm2-plugin:pm2-executor",
    "options": {
        "command": "my-nest-app:serve:production",
        "name": "example"
    }
}
Enter fullscreen mode Exit fullscreen mode

You can now run the task using the following command:

npx nx run my-nest-app:pm2
Enter fullscreen mode Exit fullscreen mode

You can monitor the service instances using the following command:

pm2 monit
Enter fullscreen mode Exit fullscreen mode

Conclusion

PM2 is a daemon process manager that will help you manage and keep your application online 24/7. It has a lot of features that will help you in the process of deploying and maintaining your application.

pm2 Article's
30 articles in total
Favicon
Deploy NestJS and NextJS application in same server using pm2 and Nginx
Favicon
Guia de Comandos PM2
Favicon
๐Ÿš€ Deploying Node.js Application with PM2, NGINX, and SSL Configuration ๐Ÿš€
Favicon
Monitoring PM2 in production
Favicon
Mastering PM2: Optimizing Node.js and Next.js Applications for Performance and Scalability
Favicon
Setting Up PM2 for Multi-User Access on Ubuntu Instance
Favicon
Manual deployment of NestJS and Angular applications on a dedicated server via "Docker Compose" and "PM2"
Favicon
Build applications on NestJS and Angular and run them in two versions: via PM2 and via Docker Compose
Favicon
An example of a simple update of NestJS-mod libraries
Favicon
Using pm2 to Manage Node.js Applications
Favicon
Using Screen and PM2 for Deploying, Debugging, and Running NestJS in Production
Favicon
Host Multiple Node Apps with nginx, pm2 with SSL certificate
Favicon
Node.js PM2 Orchestration Explained
Favicon
Managing Logs with PM2 and pm2-logrotate
Favicon
Streamlining PM2 Startup for Node.js Applications: A Comprehensive Guide
Favicon
Managing Next.js and NestJS Applications in Production with PM2
Favicon
Deploy a Full Stack Web App to VPS Server with NGINX andย PM2!
Favicon
Como utilizar o PM2 para gerenciar aplicaรงรตes
Favicon
Guide to Running a Node Server in Nx Monorepo using PM2 Process Manager
Favicon
How to run old nodejs Project into new nodejs project
Favicon
CentOS 7 NodeJS Kurulumu
Favicon
How to start node.js app with pm2
Favicon
How to start node.js app with pm2
Favicon
Deploying Multiple NodeJS Servers on a Single DigitalOcean Droplet; Managed by PM2, Without Using an ecosystem.config.js file
Favicon
Automatic deploys with PM2, Caddy, and GitHub Actions
Favicon
Utilizando PM2 (Basico)
Favicon
Experience on PM2 Tricks to manage your NodeJs processes
Favicon
Deploy Nest JS App using PM2 on Linux (Ubuntu) Server
Favicon
Install PM2 (Process Manager 2)
Favicon
Generate server block (virtual hosts) for nginx dynamically

Featured ones: