A simple yet powerful deployment script for Node.js applications running with PM2 process manager.
- π 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
- Bash shell environment (Linux, macOS, WSL on Windows)
- Node.js and npm installed
- PM2 process manager (
npm install -g pm2)
- Download the script:
curl -o nodejsdeploy.sh https://raw.githubusercontent.com/yourusername/nodejsdeploy/main/nodejsdeploy.sh- Make it executable:
chmod +x nodejsdeploy.sh- (Optional) Place it somewhere in your PATH for easy access:
sudo mv nodejsdeploy.sh /usr/local/bin/nodejsdeploy- Edit the configuration section at the top of the script to match your application.
- Run the script:
./nodejsdeploy.shThe script can be configured in two ways:
- 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"- Use command line arguments to override defaults:
./nodejsdeploy.sh --name my-app --dir ~/projects/my-app --gitDeploy from local files (no git pull):
./nodejsdeploy.sh --name my-app --dir ~/my-appPull latest changes from git before deploying:
./nodejsdeploy.sh --git --name my-api --dir ~/projects/my-api --branch main./nodejsdeploy.sh --name dashboard --dir /var/www/dashboard| 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) |
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!'")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"APP_NAME="nextjs-app"
APP_DIR="~/nextjs-app"
BUILD_CMD="npm run build"
START_CMD="npm start"APP_NAME="express-api"
APP_DIR="~/express-api"
BUILD_CMD="npm run build"
START_CMD="node dist/server.js"APP_NAME="nest-app"
APP_DIR="~/nest-app"
BUILD_CMD="npm run build"
START_CMD="node dist/main.js"- Use Environment Variables: Don't hardcode sensitive information in the script
- PM2 Ecosystem: For complex applications, consider using a PM2 ecosystem file
- Backup: Always backup your application before deployment
- Set Memory Limits: Specify memory limits for your PM2 processes
- Logging: Configure PM2 logs path for easier troubleshooting
Install PM2 globally:
npm install -g pm2Configure git credentials or use SSH keys for authentication.
Check application logs with:
pm2 logs APP_NAMEContributions are welcome! Feel free to submit a Pull Request.
MIT
Created by Kryton Labs - Making deployment simple for Node.js developers.