πŸš€ Deploy Node.js API with PM2 on Windows (Offline + Auto-Restart on Boot)

 When deploying a Node.js API on a Windows server without internet access, traditional online installation steps won’t work. This guide walks you through offline deployment with PM2 and configuring it to auto-start on reboot using pm2-windows-startup.



✅ Prerequisites

  1. A development machine with Node.js and internet access.

  2. The target Windows server has:

    • Node.js installed

    • No internet access

    • Your Node.js API project copied

πŸ“¦ Step 1: Prepare Offline PM2 Package

On a machine with internet:

bash
mkdir pm2-offline && cd pm2-offline npm install pm2

Then zip the folder:

bash
cd .. zip -r pm2-offline.zip pm2-offline

πŸ“ Step 2: Transfer to Target Server

Copy these to the Windows server:

  • pm2-offline.zip

  • Your Node.js API project

Unzip both:

bash
unzip pm2-offline.zip

πŸ”Ή 3. Use PM2 Locally or Link Globally

  • To use PM2 in that folder:

    bash
    ./node_modules/.bin/pm2 --version ./node_modules/.bin/pm2 start app.js
  • Or install it globally (optional):

    bash
    npm install -g ./node_modules/pm2

πŸ› ️ Step 3: Start API with PM2

Open Command Prompt or PowerShell and run:

bash
cd C:\path\to\your-api node C:\path\to\pm2-offline\node_modules\pm2\bin\pm2 start index.js --name your-api

Replace index.js with your actual entry file.


πŸ’Ύ Step 4: Save PM2 Process List

bash
node C:\path\to\pm2-offline\node_modules\pm2\bin\pm2 save

This saves the process list for auto-restore.


πŸ” Step 5: Auto-Start on Reboot (PM2 on Windows)

Install PM2 Windows startup module:

bash
npm install -g pm2-windows-startup

Then run:

bash
pm2-startup install

This registers PM2 as a Windows service to auto-start after reboot.


🎯 After Reboot

  • Your API starts automatically

  • It stays alive forever (auto-restarts on crash)

  • No internet required!


πŸ§ͺ Helpful PM2 Commands (Offline)

bash
# Check status node C:\path\to\pm2-offline\node_modules\pm2\bin\pm2 status # View logs node C:\path\to\pm2-offline\node_modules\pm2\bin\pm2 logs # Restart your API node C:\path\to\pm2-offline\node_modules\pm2\bin\pm2 restart your-api

πŸ’‘ Summary

TaskCommand
Start apppm2 start index.js --name your-api
Save app listpm2 save
Auto-start on rebootpm2-startup install
Run offline PM2 commandsnode path\to\pm2\bin\pm2 <command>

πŸ“ Final Tip

For even smoother startup, you can create a .bat file that restores the saved PM2 processes on boot, or configure it in Windows Task Scheduler if needed.

Comments