Skip to content

Simple script to automate stopping, deploying, starting your NodeJS PM2 app

License

Notifications You must be signed in to change notification settings

kmanan/nodejsdeploy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Node.js Deployment Script

A simple yet powerful deployment script for Node.js applications running with PM2 process manager.

Script Banner PM2 Bash

Features

  • πŸš€ Simple Deployment: Deploy your Node.js application with a single command
  • πŸ”„ Multiple Deployment Methods: Support for both local and git-based deployments
  • πŸ› οΈ Customizable: Configure all aspects of the deployment process
  • πŸͺ Hooks: Add custom commands to run before or after deployment
  • πŸ” Logging: Clear, colorful output showing each step of the process
  • πŸ§ͺ Error Handling: Robust error detection and reporting
  • πŸ“Š Status Monitoring: Verify application status after deployment

Prerequisites

  • Bash shell environment (Linux, macOS, WSL on Windows)
  • Node.js and npm installed
  • PM2 process manager (npm install -g pm2)

Installation

  1. Download the script:
curl -o nodejsdeploy.sh https://raw.githubusercontent.com/yourusername/nodejsdeploy/main/nodejsdeploy.sh
  1. Make it executable:
chmod +x nodejsdeploy.sh
  1. (Optional) Place it somewhere in your PATH for easy access:
sudo mv nodejsdeploy.sh /usr/local/bin/nodejsdeploy

Quick Start

  1. Edit the configuration section at the top of the script to match your application.
  2. Run the script:
./nodejsdeploy.sh

Configuration

The script can be configured in two ways:

  1. Edit the configuration section at the top of the script:
# Application name (used for PM2)
APP_NAME="my-nodejs-app"

# Application directory (absolute path or relative to home)
APP_DIR="~/my-nodejs-app"

# Build command (default: npm run build)
BUILD_CMD="npm run build"

# Install command (default: npm install)
INSTALL_CMD="npm install --legacy-peer-deps"

# Start command (default: npm start)
START_CMD="npm start"

# Deployment method: "local" or "git"
DEPLOY_METHOD="local"
  1. Use command line arguments to override defaults:
./nodejsdeploy.sh --name my-app --dir ~/projects/my-app --git

Usage Examples

Basic Local Deployment

Deploy from local files (no git pull):

./nodejsdeploy.sh --name my-app --dir ~/my-app

Git-Based Deployment

Pull latest changes from git before deploying:

./nodejsdeploy.sh --git --name my-api --dir ~/projects/my-api --branch main

With Custom Application Directory

./nodejsdeploy.sh --name dashboard --dir /var/www/dashboard

Command Line Options

Option Description
-h, --help Display help message
-l, --local Use local deployment method (default)
-g, --git Use git deployment method
-n, --name NAME Set application name for PM2
-d, --dir PATH Set application directory
-b, --branch BRANCH Set git branch (for git method)

Advanced Configuration

Custom Commands

You can define custom commands to run before or after the deployment process:

# Custom pre-deploy commands
PRE_DEPLOY_CMDS=("npm run clean" "npm run lint")

# Custom post-deploy commands
POST_DEPLOY_CMDS=("npm run db:migrate" "echo 'Deployment complete!'")

Custom Start Command

If your application uses a custom start command (not npm start):

# For Next.js applications
START_CMD="npm start -- -p 3000"

# For custom Node scripts
START_CMD="node server.js"

Common Use Cases

Next.js Application

APP_NAME="nextjs-app"
APP_DIR="~/nextjs-app"
BUILD_CMD="npm run build"
START_CMD="npm start"

Express API Server

APP_NAME="express-api"
APP_DIR="~/express-api"
BUILD_CMD="npm run build"
START_CMD="node dist/server.js"

NestJS Application

APP_NAME="nest-app"
APP_DIR="~/nest-app"
BUILD_CMD="npm run build"
START_CMD="node dist/main.js"

Best Practices

  1. Use Environment Variables: Don't hardcode sensitive information in the script
  2. PM2 Ecosystem: For complex applications, consider using a PM2 ecosystem file
  3. Backup: Always backup your application before deployment
  4. Set Memory Limits: Specify memory limits for your PM2 processes
  5. Logging: Configure PM2 logs path for easier troubleshooting

Troubleshooting

Script fails with "PM2 is not installed"

Install PM2 globally:

npm install -g pm2

Git pull fails with authentication error

Configure git credentials or use SSH keys for authentication.

Application crashes after deployment

Check application logs with:

pm2 logs APP_NAME

Contributing

Contributions are welcome! Feel free to submit a Pull Request.

License

MIT


Created by Kryton Labs - Making deployment simple for Node.js developers.

About

Simple script to automate stopping, deploying, starting your NodeJS PM2 app

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages