6.18 How to install and deploy a NodeJS project

Banner

Node.js is a powerful platform for creating fast and scalable network applications. Based on Google Chrome’s V8 engine, this technology allows using JavaScript on the server side, opening new possibilities for developers.

In today’s world, Node.js has become an integral part of the ecosystem, allowing developers to create everything from simple web servers to complex microservice architectures. Its event-driven architecture and non-blocking I/O make it particularly effective for real-time applications.

A significant advantage of Node.js is its huge developer community and rich ecosystem of npm (Node Package Manager) packages, which significantly accelerates the development process and implementation of new features.

What is Node.js and why is it needed?

Node.js is used for:

  • Creating web servers and APIs.
  • Developing real-time applications (chats, games).
  • Creating command-line tools.
  • Processing streaming data.
  • Microservice architecture.

Important: before installing Node.js, make sure You have root administrator rights on the server. It is also recommended to create a backup of important data before making significant changes to the system.

Installing Node.js

Node.js can be installed in several ways. The simplest method is through the operating system’s standard repositories. This method is suitable for quick start and testing but may not provide the latest version of Node.js. Let’s examine the installation example for Ubuntu/Debian and CentOS OS.

During installation, two main components will be installed:

  • Node.js – JavaScript runtime environment.
  • npm (Node Package Manager) – package manager that allows installing additional modules.

The simplest way to install is through standard repositories. Let’s examine the installation example for different OS.

Ubuntu/Debian:

Update package list

  1. sudo apt update

Install Node.js and npm

  1. sudo apt install nodejs npm -y

Check version:

  1. node --version
  2. npm --version
CentOS/AlmaLinux/RockyLinux:

Update system:

  1. sudo yum update

Install epel-release:

  1. sudo yum install epel-release

Install Node.js and npm:

  1. sudo yum install nodejs npm

Check versions

  1. node --version
  2. npm --version

We inform You: when installing through apt or yum, You may not get the latest version of Node.js. If You need a specific or the latest version, it is recommended to use nvm.

Installation through nvm (Node Version Manager) provides a more flexible approach to managing Node.js versions. This tool is particularly useful when You need to:

  • Work with different projects requiring different Node.js versions.
  • Test code on different Node.js versions.
  • Easily upgrade to new versions.
  • Avoid permission issues when installing global packages.

The nvm installation process includes the following steps:

  1. Downloading the installation script.
  2. Configuring environment variables.
  3. Installing the required Node.js version.

Installation procedure through nvm (Node Version Manager).

Advantages of using nvm:

  • Easy switching between Node.js versions.
  • Ability to install latest versions.
  • Isolated environments for different projects.
  • Doesn’t require sudo for package installation.
Ubuntu/Debian:

Install nvm:

  1. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

Reload shell profile:

  1. source ~/.bashrc

View available versions:

  1. nvm list-remote

Install latest Node.js version:

  1. nvm install node

Install specific version:

  1. nvm install 22

Switch between versions:

  1. nvm use 22

Set default version:

  1. nvm alias default 22
CentOS/AlmaLinux/RockyLinux:

Install required dependencies:

  1. sudo yum install curl git -y

Download and install nvm:

  1. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

Add nvm to current session:

  1. export NVM_DIR="$HOME/.nvm"
  2. [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
  3. [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

Reload profile:

  1. source ~/.bashrc

Check nvm installation:

  1. nvm --version

Install latest Node.js version:

  1. nvm install node

Or install specific version:

  1. nvm install 22

Project Deployment

Reminder: when developing commercial projects, it is recommended to use LTS (Long Term Support) versions of Node.js to ensure stability and security.

Web Server Configuration (Nginx)

For Node.js application in production, it is recommended to use a web server as a reverse proxy. There are several popular options:

  • Nginx – lightweight and high-performance web server.
  • Apache – reliable and time-tested web server.
  • Caddy – modern web server with automatic HTTPS support.
  • HAProxy – specialized load balancing solution.

In our example, we’ll look at configuring Nginx, as one of the most popular solutions.

Installing Nginx:

Ubuntu/Debian:
  1. sudo apt install nginx -y
CentOS/AlmaLinux/RockyLinux:
  1. sudo yum install nginx -y

Proxy configuration

  1. sudo nano /etc/nginx/sites-available/your-app

Example Nginx configuration:

server {
    # Specify the port on which Nginx will listen for incoming connections
    listen 80;
    
    # Specify server domain name
    server_name your-domain.com;

    # Logging configuration
    # access_log - records all successful server requests
    access_log /var/log/nginx/your-app.access.log;
    # error_log - records errors and warnings
    error_log /var/log/nginx/your-app.error.log;

    location / {
        # Specify the address where Nginx will redirect requests
        # In this case to a local Node.js server on port 3000
        proxy_pass http://localhost:3000;

        # Specify HTTP protocol version
        proxy_http_version 1.1;

        # WebSocket support settings
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';

        # Pass original host in header
        proxy_set_header Host $host;

        # Disable caching
        proxy_cache_bypass $http_upgrade;
        
        # Timeout settings
        # Maximum time for establishing connection
        proxy_connect_timeout 60s;
        # Maximum time waiting to send data to client
        proxy_send_timeout 60s;
        # Maximum time waiting to read data from server
        proxy_read_timeout 60s;
    }
}

It’s important to understand key configuration points:

  • Proxying allows the Node.js application to work on an internal port while Nginx handles all external requests.
  • WebSocket settings are necessary for real-time functionality.
  • Proper log configuration will help in debugging problems.
  • Timeouts protect the server from hung connections.

Reminder: in production environment, it is also recommended to configure:

  • Response compression (gzip).
  • Static file caching.
  • SSL/TLS certificate.
  • Request size limits.

Application Launch

After installing Node.js and configuring the web server, it’s necessary to ensure reliable launch and maintenance of Your application. For this, we’ll use PM2 – a process manager for Node.js applications.

PM2 provides the following capabilities:

  • Automatic application restart after crashes.
  • Resource and log monitoring.
  • Load balancing.
  • Environment variable management.
  • Auto-start on server reboot.

Installing PM2:

  1. sudo npm install -g pm2

Starting the application:

  1. cd /path/to/your/app
  2. pm2 start app.js

Setting up auto-start:

  1. pm2 startup
  2. pm2 save

Additionally: after setting up auto-start through PM2, we recommend:

  1. Reboot the server and verify that the application started.
  2. Check process status through pm2 list.
  3. View logs through pm2 logs.

The following are the main PM2 commands for managing a Node.js application.

Start application:

  1. pm2 start app.js --name "your-app"

Stop:

  1. pm2 stop "your-app"

Restart:

  1. pm2 restart "your-app"

View logs:

  1. pm2 logs "your-app"

Process monitoring:

  1. pm2 monit

Conclusion

Node.js provides powerful tools for creating modern web applications. With proper configuration and maintenance, Your application will work stably and efficiently. Regular component updates and performance monitoring will help ensure the security and reliability of Your service.

To ensure stable operation of Your application, it is recommended to:

  • Regularly update Node.js and npm packages.
  • Use logging to track errors.
  • Configure system resource monitoring.
  • Regularly create backups.