Node.js Deployment Guide

Here’s a comprehensive guide on Node.js Deployment, covering all common methods, best practices, and step-by-step instructions to deploy your Node.js application to production.


🚀 Node.js Deployment Guide

Deploying Node.js applications requires moving your app from development (local machine) to production (server, cloud, or container) safely and efficiently.


1️⃣ Prepare Your App for Deployment

Before deploying, ensure:

  • package.json has proper scripts:



 

  • Use environment variables for secrets (.env)

  • Optimize app for production:

    • Minify frontend assets (if any)

    • Enable compression

    • Use clustering for multi-core servers


2️⃣ Choose Deployment Target

TargetDescriptionProsCons
VPS (DigitalOcean, Linode)Full server controlFull control, low costSetup & maintenance required
PaaS (Heroku, Render, Railway)Managed deploymentEasy & fastLimited resources, may be costly
Cloud (AWS EC2, GCP, Azure)Custom cloud VMScalable, full controlRequires cloud knowledge
Docker + KubernetesContainerized appsScalable, portableComplex setup
Serverless (AWS Lambda, Vercel)Functions-as-a-ServicePay-per-use, automatic scalingCold start, limited runtime

3️⃣ Deployment Methods

3.1 Deploy on VPS (Ubuntu)

Step 1: SSH to server



 

Step 2: Install Node.js



 

Step 3: Upload project

  • Use git clone or scp to upload your code

Step 4: Install dependencies

npm install --production

Step 5: Environment variables

  • Set .env file or export variables:



 

Step 6: Start app with PM2



 


3.2 Deploy on Heroku

Step 1: Install Heroku CLI

curl https://cli-assets.heroku.com/install.sh | sh

Step 2: Login

heroku login

Step 3: Create app

heroku create my-node-app

Step 4: Push code

git push heroku main

Step 5: Set environment variables

heroku config:set NODE_ENV=production
heroku config:set DB_HOST=yourdbhost

3.3 Deploy with Docker

Step 1: Create Dockerfile

FROM node:20
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]

Step 2: Build & run

docker build -t my-node-app .
docker run -d -p 3000:3000 --env-file .env my-node-app

Step 3: Optional Docker Compose

version: '3'
services:
app:
build: .
ports:
- "3000:3000"
env_file:
- .env

3.4 Deploy on Serverless / Cloud

  • AWS Lambda with API Gateway

  • Vercel (for Next.js + Node APIs)

  • Render / Railway (simple Node.js hosting)

Steps usually involve pushing code to GitHub and connecting the repository to the cloud service for automatic deployment.


4️⃣ Production Best Practices

  • Process Manager: PM2, forever

  • Logging: Winston, Bunyan, or Logrotate

  • Monitoring: PM2 monitoring, Sentry, New Relic

  • Security: HTTPS, Helmet, Rate Limiter, CSRF protection

  • Environment variables: .env or server secrets

  • Cluster mode: Use all CPU cores

  • Backup: Database and environment backup


5️⃣ Optional: CI/CD Integration

Combine deployment with CI/CD pipelines (GitHub Actions, GitLab CI) for:

  • Automated testing

  • Linting

  • Build

  • Deployment to server

Example: Push to main → run tests → deploy via PM2


6️⃣ Monitoring & Scaling

  • Scaling: Use PM2 cluster or Docker + Kubernetes

  • Health checks: Implement /health route

  • Alerts: Monitor CPU, memory, errors


🎯 Summary

  • Node.js apps can be deployed to VPS, PaaS, Docker, or serverless

  • Use PM2 for process management

  • Always use environment variables for sensitive data

  • Enable security, logging, monitoring, and scaling

  • CI/CD pipelines make deployment automated and safe

You may also like...