In short
To host a discord.js bot on a VDS, install the Node.js LTS release on the server, transfer your code with Git or SFTP, store the token in a .env file and install dependencies with npm ci. Then start the bot with PM2 and run pm2 save and pm2 startup; the bot comes back up automatically whether it crashes or the server reboots.
In this guide
- What you need before you start
- 1. Prepare the server and create a bot user
- 2. Install Node.js
- 3. Transfer your bot files to the server
- 4. Store the token in a .env file
- 5. Run it 24/7 with PM2
- 6. Follow the logs and monitor the bot
- 7. Update the bot
- 8. Track memory and performance
- Common errors
- Next step: Pick the right VDS for your bot
Hosting a discord.js bot on a VDS comes down to four basic steps: installing Node.js on the server, transferring your bot files, storing the token safely in a .env file and running the bot with PM2. PM2 keeps the bot running in the background, restarts it when it crashes and brings it back automatically when the server reboots. The commands below are written for Ubuntu 22.04/24.04 and Debian 12.
What you need before you start
- A Linux VDS with root access. For simple and mid-sized bots, the OB-DISCORD-1 with 2 vCPU and 2 GB RAM is a solid starting point.
- A bot token from the Discord Developer Portal.
- A discord.js project that runs on your computer (with
package.jsonand, ideally,package-lock.json). - SSH access via PowerShell or PuTTY on Windows, or the terminal on macOS and Linux.
1. Prepare the server and create a bot user
Connect over SSH as root, update the system and create a separate user to run the bot. Not running the bot as root makes it much harder to take over the whole server, even if your code has a vulnerability.
ssh root@SUNUCU_IP
apt update && apt upgrade -y
apt install -y curl git ufw
adduser --disabled-password --gecos "" botuserThe bot doesn't accept inbound connections; it only opens a connection to Discord. So you don't need to open any ports in the firewall besides SSH:
ufw allow OpenSSH
ufw enableFor details on SSH keys and firewall settings, see our game VDS security guide.
2. Install Node.js
The Node.js version in your distribution's default repositories is often too old for current discord.js. Install the current LTS (Long-Term Support) release from the NodeSource repository:
curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
apt install -y nodejs
node -v
npm -vThe current version of discord.js requires a minimum Node.js version; as long as you use the LTS release, you'll meet it. You can find the exact requirement in the discord.js documentation.
3. Transfer your bot files to the server
The cleanest method is pulling the code from a private Git repository; updates then take a single command.
su - botuser
git clone https://github.com/kullanici-adiniz/benim-botum.git
cd benim-botumIf you don't use Git, upload the files as root over SFTP with WinSCP or FileZilla to the /home/botuser/benim-botum folder and give ownership to the bot user:
chown -R botuser:botuser /home/botuser/benim-botumNever upload the node_modules folder. It contains thousands of files, so the transfer takes forever, and some packages compiled on Windows won't run on Linux. Install dependencies on the server instead:
cd ~/benim-botum
npm ci --omit=devnpm ci installs the exact versions in your package-lock.json file. If you don't have a lockfile, use npm install --omit=dev.
4. Store the token in a .env file
The token is your bot's password. It should never be hardcoded or pushed to your Git repository. Create a .env file in the project folder:
nano .envDISCORD_TOKEN=buraya_bot_tokeniniz
CLIENT_ID=uygulama_kimliginizRestrict the file so only the bot user can read it, and make sure your .gitignore file contains .env and node_modules lines:
chmod 600 .envIn your code, read the token from an environment variable. Here's an example entry file using the dotenv package:
require('dotenv').config();
const { Client, Events, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once(Events.ClientReady, (c) => {
console.log(`Giriş yapıldı: ${c.user.tag}`);
});
client.login(process.env.DISCORD_TOKEN);On Node.js 20.6 and later, you can also run it without the dotenv package using node --env-file=.env index.js. We explain what to do if your token ever leaks in our article on Discord bot token security.
Before moving to PM2, start the bot manually once, confirm it shows as online in Discord and stop it with Ctrl+C:
node index.js5. Run it 24/7 with PM2
PM2 is a process manager for Node.js applications. It runs the bot in the background, restarts it if it crashes, writes logs to files and starts it automatically on boot. Install it globally as root:
npm install -g pm2Keeping the settings in a file makes future reinstalls easier. Create ecosystem.config.js in the project folder:
module.exports = {
apps: [{
name: 'benim-botum',
script: 'index.js',
cwd: '/home/botuser/benim-botum',
instances: 1,
exec_mode: 'fork',
max_memory_restart: '400M',
exp_backoff_restart_delay: 100,
env: { NODE_ENV: 'production' }
}]
};- instances: 1 and fork mode: Don't run a Discord bot as multiple copies in cluster mode. Each copy logs in separately with the same token, and commands get answered more than once.
- max_memory_restart: When a bot with a memory leak goes over this limit, PM2 restarts it. Set the value a bit above your bot's normal usage.
- exp_backoff_restart_delay: If the bot keeps crashing, the wait between restarts grows, so you don't hit Discord with dozens of login attempts per second.
Start it as the bot user and save the process list:
su - botuser
cd ~/benim-botum
pm2 start ecosystem.config.js
pm2 saveTo have PM2 start when the server reboots, create the startup service as root:
pm2 startup systemd -u botuser --hp /home/botuserThis command creates a systemd service named pm2-botuser. To test it, reboot the server with reboot and run pm2 status as the bot user; the bot should show as online.
6. Follow the logs and monitor the bot
Most debugging happens in the logs. Commonly used commands:
pm2 status
pm2 logs benim-botum --lines 100
pm2 monit
pm2 describe benim-botumIf the pm2 describe output shows the restarts count climbing fast, the bot is stuck in a crash loop because of an error. Install the rotation module so logs don't fill up the disk over time:
pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 77. Update the bot
When you change the code, these steps as the bot user are all you need:
cd ~/benim-botum
git pull
npm ci --omit=dev
pm2 restart benim-botumIf you changed the structure of your slash commands, remember to run your command registration (deploy) script once as well. If you use the structure from the discord.js guide, it's usually this command:
node deploy-commands.jsDon't register commands automatically every time the bot starts. Discord rate-limits command registration requests, and if the bot crashes and restarts repeatedly, you'll hit that limit for nothing. The safest approach is to run registration manually only when a command's name, description or options change. Server-specific (guild) registrations show up instantly for testing, while global registrations can take a while to propagate to all servers.
8. Track memory and performance
After the bot has run for a few days, look at its real resource usage. For the server's overall state and the bot's own usage:
free -h
pm2 status
pm2 monitIn the pm2 status output, the mem column shows the bot's current memory usage. If that value keeps climbing throughout the day and never drops, there's a memory leak in your code. In discord.js, the most common cause is caches that grow without limit; you can cap the message and member caches with the makeCache and sweepers options when creating the client. Until you find a real fix, the max_memory_restart setting keeps the bot within a safe limit.
To run a second bot on the same server, just create a separate folder and add a new apps entry to the ecosystem file. Each bot should have its own name, cwd and .env file; remember to run pm2 save again after adding it. Work out your total RAM needs by adding up each bot's values from the pm2 status output plus a safety margin.
Common errors
| Error | Cause | Fix |
|---|---|---|
| Used disallowed intents | The code requests a privileged intent such as Message Content or Server Members, but it isn't enabled in the portal | Enable the intent on the Bot tab in the Developer Portal |
| TokenInvalid / An invalid token was provided | .env isn't being read or the token was reset | Check the variable name, file path and cwd setting |
| Status errored in PM2 | The bot crashes on startup | Read the first line of the error with pm2 logs |
| Bot doesn't start after a reboot | pm2 save or pm2 startup is missing | Run both commands again |
If your bot goes offline regularly, see our Discord bot keeps going offline guide for a systematic checklist.
Next step: Pick the right VDS for your bot
This setup works on any Linux VDS with root access. OyunBulut's OB-DISCORD-1 bot VDS, with 2 vCPU, 2 GB RAM, 30 GB NVMe, KVM virtualization, layered DDoS protection, automatic setup and root access, fits this scenario exactly. If you'll run multiple bots, Lavalink or a database on the same server, compare the higher-RAM Discord Bot VDS plans; if you get stuck during setup, you can ask for help in Turkish through a support ticket.
Frequently asked questions
Can a discord.js bot run 24/7 without PM2?
A bot started with node index.js stops when the SSH session closes or it throws an error. A process manager such as PM2 or systemd keeps the bot running in the background and restarts it automatically when it crashes.
What does the pm2 startup command do?
It creates a systemd service for PM2, so when the server reboots, PM2 and the apps you saved with pm2 save start automatically.
Where should I store my Discord bot token?
Store the token in a .env file in your project folder, restrict the file permissions with chmod 600 and add .env to your .gitignore. The token should never be written into your code or your Git repository.
Should I upload the node_modules folder to the server?
No. Transferring node_modules is slow, and packages compiled on Windows may not run on Linux. Install dependencies on the server with the npm ci command.
Does PM2 cluster mode make a Discord bot faster?
No, cluster mode causes problems for Discord bots because each copy logs in separately with the same token. To scale large bots, use discord.js's own sharding method.
How much RAM is enough for a discord.js bot?
A simple discord.js bot usually runs on a few hundred MB. Needs grow as you add a database or music features; 2 GB RAM is a comfortable starting point for most small and mid-sized bots.