Node.jsDiscord.jsDeploy5 min read

Deploy Discord bot with Node.js

Upload and run your Discord bot in Node.js with automatic restart and persistent logs.


Prerequisites

  • VPS or Discord Bot hosting plan at Baires Host
  • Your bot token from the Discord Developer Portal
  • Bot code ready to deploy
  • SSH access to your server

Step 1: Install Node.js with nvm

bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 20
nvm use 20
node --version

Step 2: Install PM2

PM2 keeps your bot running 24/7 with automatic restarts:

bash
npm install -g pm2

Step 3: Upload your bot code

Option A - Clone from Git:

bash
mkdir -p /opt/bots
cd /opt/bots
git clone https://github.com/youruser/your-bot.git
cd your-bot
npm install --production

Option B - Upload via SCP:

bash
# From your local machine
scp -r ./your-bot deploy@YOUR_IP:/opt/bots/

Step 4: Configure environment variables

Create a .env file for sensitive data:

bash
nano .env
env
DISCORD_TOKEN=your_bot_token_here
CLIENT_ID=your_client_id
GUILD_ID=your_test_guild_id
PREFIX=!
NODE_ENV=production

Important: Never commit your .env file to Git. Add it to .gitignore.

Step 5: Start with PM2

bash
pm2 start src/index.js --name "my-bot"
pm2 save

Or with an ecosystem file for more control:

bash
nano ecosystem.config.js
javascript
module.exports = {
  apps: [{
    name: 'my-bot',
    script: './src/index.js',
    env: {
      NODE_ENV: 'production'
    },
    max_memory_restart: '300M',
    restart_delay: 5000,
    max_restarts: 10,
    log_date_format: 'YYYY-MM-DD HH:mm:ss',
    error_file: './logs/error.log',
    out_file: './logs/output.log'
  }]
};
bash
pm2 start ecosystem.config.js
pm2 save

Step 6: Enable auto-start on boot

bash
pm2 startup systemd
# Run the command it outputs
pm2 save

Step 7: Create a deploy script

Automate updates:

bash
nano deploy.sh
bash
#!/bin/bash
echo "Pulling latest changes..."
git pull origin main

echo "Installing dependencies..."
npm install --production

echo "Restarting bot..."
pm2 restart my-bot

echo "Deploy complete: $(date)"
pm2 status
bash
chmod +x deploy.sh

Alternative: systemd service

If you prefer systemd over PM2:

bash
sudo nano /etc/systemd/system/discord-bot.service
ini
[Unit]
Description=Discord Bot
After=network.target

[Service]
User=deploy
WorkingDirectory=/opt/bots/your-bot
ExecStart=/home/deploy/.nvm/versions/node/v20.11.0/bin/node src/index.js
Restart=always
RestartSec=10
EnvironmentFile=/opt/bots/your-bot/.env

[Install]
WantedBy=multi-user.target
bash
sudo systemctl daemon-reload
sudo systemctl enable discord-bot
sudo systemctl start discord-bot

Useful PM2 commands

bash
pm2 status           # View all processes
pm2 logs my-bot      # View real-time logs
pm2 logs my-bot --lines 100  # Last 100 lines
pm2 restart my-bot   # Restart
pm2 stop my-bot      # Stop
pm2 monit            # Real-time dashboard

Troubleshooting

  • Bot goes offline: Check pm2 logs for errors
  • Memory issues: Set max_memory_restart in ecosystem config
  • Token invalid: Verify your .env file and regenerate the token if needed
  • Missing intents: Ensure you've enabled required intents in the Developer Portal

Was this guide helpful?