---
title: "Deploy a Discord bot with Node.js and PM2: env vars and auto restart | StreetHosting"
description: "Run a discord.js bot in production with PM2 on Linux: ecosystem.config.cjs, optional cluster mode, logs, systemd startup and good practices for secrets."
url: "https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2"
type: "page"
language: "en-US"
---

Discord Bots · 4 min · Intermediate

Published on May 19, 2026 · Updated on May 20, 2026

# Deploy a Discord bot with Node.js and PM2

PM2 is a popular Node process manager for Discord bots because it restarts after a crash, survives reboots when paired with the startup hook and centralizes basic logs without standing up a whole Kubernetes cluster. This guide covers the ecosystem file, env vars for the token and how it fits hosting in Brazil.

By [Equipe StreetHosting](https://streethosting.com.br/en/autores#equipe-streethosting) · StreetHosting infrastructure and support team

[Deploying and running apps](https://streethosting.com.br/en/guides/topics/deploy) [Linux administration](https://streethosting.com.br/en/guides/topics/linux) [Automation and webhooks](https://streethosting.com.br/en/guides/topics/automation)

Summarize with:

[](https://chat.openai.com/?q=Summarize%20the%20key%20points%20of%20this%20StreetHosting%20guide%3A%20https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fdeploy-discord-bot-pm2.%20Highlight%20the%20step-by-step%20instructions%2C%20the%20prerequisites%20and%20the%20most%20common%20mistakes. "ChatGPT") [](https://claude.ai/new?q=Summarize%20the%20key%20points%20of%20this%20StreetHosting%20guide%3A%20https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fdeploy-discord-bot-pm2.%20Highlight%20the%20step-by-step%20instructions%2C%20the%20prerequisites%20and%20the%20most%20common%20mistakes. "Claude") [](https://www.google.com/search?udm=50&aep=11&q=Summarize%20the%20key%20points%20of%20this%20StreetHosting%20guide%3A%20https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fdeploy-discord-bot-pm2.%20Highlight%20the%20step-by-step%20instructions%2C%20the%20prerequisites%20and%20the%20most%20common%20mistakes. "Google AI Mode") [](https://x.com/i/grok?text=Summarize%20the%20key%20points%20of%20this%20StreetHosting%20guide%3A%20https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fdeploy-discord-bot-pm2.%20Highlight%20the%20step-by-step%20instructions%2C%20the%20prerequisites%20and%20the%20most%20common%20mistakes. "Grok") [](https://www.perplexity.ai/search/new?q=Summarize%20the%20key%20points%20of%20this%20StreetHosting%20guide%3A%20https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fdeploy-discord-bot-pm2.%20Highlight%20the%20step-by-step%20instructions%2C%20the%20prerequisites%20and%20the%20most%20common%20mistakes. "Perplexity")

Share:

[](https://x.com/intent/tweet?text=Deploy%20a%20Discord%20bot%20with%20Node.js%20and%20PM2%3A%20env%20vars%20and%20auto%20restart&url=https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fdeploy-discord-bot-pm2 "Share on X") [](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fdeploy-discord-bot-pm2 "Share on Facebook") [](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fdeploy-discord-bot-pm2 "Share on LinkedIn") [](https://wa.me/?text=Deploy%20a%20Discord%20bot%20with%20Node.js%20and%20PM2%3A%20env%20vars%20and%20auto%20restart%20https%3A%2F%2Fstreethosting.com.br%2Fen%2Fguides%2Fdiscord-bots%2Fdeploy-discord-bot-pm2 "Share on WhatsApp")

For agents: Copy as Markdown [.md](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2.md)

[Previous 24/7 Discord bot hosting in Brazil](https://streethosting.com.br/en/guides/discord-bots/discord-bot-hosting-24-7) [Next How to create a Discord bot with slash commands](https://streethosting.com.br/en/guides/discord-bots/create-discord-bot-slash-commands)

In this guide 5 sections

* [Why PM2 and not just screen](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#por-que-pm2-e-nao-screen)
* [Node bootstrap and the bot file](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#bootstrap-node-e-arquivo-do-bot)
* [ecosystem.config and environment variables](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#ecosystem-config-e-env)
* [Logs, restarts and memory limits](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#logs-restarts-e-limites)
* [Managed hosting vs VPS](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#hospedagem-gerenciada-vs-vps)

Quick answer

To **deploy a Discord bot with Node.js and PM2**: install Node LTS via nvm or the official repos, copy the project to the server, set `DISCORD_TOKEN` and flags in **.env**, create an `ecosystem.config.cjs` that points at the entry script, run `pm2 start ecosystem.config.cjs`, enable `pm2 save` and `pm2 startup` to survive a reboot, monitor `pm2 logs --lines 100` and cap RAM with `max_memory_restart`. For less sysadmin work, use [StreetHosting bot hosting](https://streethosting.com.br/en/bots).

## Why PM2 and not just screen[](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#por-que-pm2-e-nao-screen)

screen and tmux help with a manual session, but they do not manage the process lifecycle. PM2 tracks state, restarts the bot when it exits with an error and gives you a single command to see logs, memory and restart count. In daily operation that cuts response time when someone reports a broken command on Discord.

If your goal is continuous uptime, pair this deploy guide with the guide on [24/7 Discord bot hosting](https://streethosting.com.br/en/guides/discord-bots/discord-bot-hosting-24-7) to define monitoring and capacity.

## Node bootstrap and the bot file[](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#bootstrap-node-e-arquivo-do-bot)

1. Connect with `ssh usuario@vps` using a key and a non-root user.
2. Install Node LTS and check the version with `node -v`.
3. Bring the project over with a private clone or a CI pipeline, without sending secrets.
4. Run `npm ci --omit=dev` and execute the build to generate `dist/`.
5. Test locally on the server with `node dist/index.js` before starting it under PM2.

The steps for secure SSH are in [secure SSH on a Linux VPS](https://streethosting.com.br/en/guides/vps/secure-ssh-linux-vps).

## ecosystem.config and environment variables[](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#ecosystem-config-e-env)

In `ecosystem.config.cjs`, declare the app name, the path to the compiled script and the absolute project directory. A common example: `name: discord-bot-prod`, `script: ./dist/index.js`, `cwd: /home/bot/app` and `env.NODE_ENV=production`. This avoids restarting in the wrong folder and makes it easy to tell production from staging by using separate names in `pm2 list`.

| Key            | Example            | Notes                             |
| -------------- | ------------------ | --------------------------------- |
| DISCORD\_TOKEN | xxx.yyy.zzz        | Rotate it if you suspect a leak   |
| NODE\_ENV      | production         | Avoids development behavior       |
| CLIENT\_ID     | 123456789012345678 | Needed to register slash commands |

If the token is exposed in a commit or a log, regenerate it in the Discord portal and restart the process with `pm2 restart discord-bot-prod --update-env`.

## Logs, restarts and memory limits[](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#logs-restarts-e-limites)

Set `max_memory_restart` to stop a memory leak before the kernel kills processes. For a mid-sized bot, starting at 300M to 500M usually works, but adjust it to the real RSS. Turn on log rotation and follow `pm2 logs --lines 100` after a deploy to make sure login and command registration finished without errors.

Example of a healthy log: `[Gateway] Logged in as ModBot#1234`. Example of a warning sign: `Error: Used disallowed intents` followed by continuous restarts. When the count in `pm2 show discord-bot-prod` goes past 5 restarts within a few minutes, treat it as an incident and freeze new deploys until the cause is fixed.

For rate limits, prefer a queue with retry and backoff. A typical log includes `429 Too Many Requests` and a `retry_after` field. Use that value to schedule the next attempt without creating a loop.

## Managed hosting vs VPS[](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#hospedagem-gerenciada-vs-vps)

On a VPS you control the whole system, including updates, firewall and hardening. With managed hosting you spend less time on maintenance and more time on the bot's code. Choose [StreetHosting Discord bot plans](https://streethosting.com.br/en/bots) when you want assisted operation. If you would rather build everything yourself, use a [Ryzen VPS](https://streethosting.com.br/en/vps/ryzen); containerized stacks can follow [Docker on an Ubuntu VPS](https://streethosting.com.br/en/guides/vps/install-docker-ubuntu-vps).

* Managed: a panel and support for the deploy and restart routine.
* VPS: freedom to run multiple bots and auxiliary services.
* Run pm2 save after changing the ecosystem file.
* Validate a reboot within a window announced to the team.
* Check status with pm2 status and pm2 logs after a restart.
* Keep a policy for dependency updates and token rotation.

In this guide

* [Why PM2 and not just screen](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#por-que-pm2-e-nao-screen)
* [Node bootstrap and the bot file](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#bootstrap-node-e-arquivo-do-bot)
* [ecosystem.config and environment variables](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#ecosystem-config-e-env)
* [Logs, restarts and memory limits](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#logs-restarts-e-limites)
* [Managed hosting vs VPS](https://streethosting.com.br/en/guides/discord-bots/deploy-discord-bot-pm2#hospedagem-gerenciada-vs-vps)

## Frequently asked questions

Does PM2 replace Docker?

For a single bot on a simple VPS, often yes. Docker shines when you need multi-service reproducibility and CI pipelines identical to production.

Can I use cluster mode for a Discord bot?

A single discord.js process is the common case; sharding for massive bots is a different chapter. PM2 cluster mode does not replace proper sharding for the Discord API.

Where do I store DISCORD\_TOKEN?

In a .env file kept out of Git with restricted permissions, or a variable exported by systemd or the hosting panel. Never console.log the token in an error.

Do I need to build TypeScript first?

Yes if the source is TS: run tsc or an equivalent pipeline before PM2 points at dist/. ts-node is an alternative for development only.

Next step

See bot plans

24/7 Discord bots with fast activation and support in Portuguese.

[See bot plans](https://streethosting.com.br/en/bots)

[See Ryzen VPS Ryzen 9 9950X VPS in São Paulo with root access, NVMe and gamer Anti-DDoS.](https://streethosting.com.br/en/vps/ryzen)

## Related guides

[Discord bots Beginner 24/7 Discord bot hosting in Brazil Discord bot hosting 24/7 combines a process manager with automatic restart, a token kept out of your code, logs and a server in Brazil for low latency to the API and your players. 6 min Read guide](https://streethosting.com.br/en/guides/discord-bots/discord-bot-hosting-24-7) [VPS Intermediate How to secure SSH on a Linux VPS: keys, passwords, fail2ban SSH is usually the first target on any VPS with a public IP. This guide walks through a practical routine that cuts the risk without complicating your day: an ED25519 key, password-free login, admin access through sudo, blocking of automated attempts and a periodic review of authorized keys. 4 min Read guide](https://streethosting.com.br/en/guides/vps/secure-ssh-linux-vps) [VPS Intermediate How to install Docker on Ubuntu 22.04 or 24.04 on a VPS (straight to the point) A stable Docker install on an Ubuntu VPS depends on the right package source and step by step validation. In this guide you set up the official repository, install Engine and Compose, test the daemon and apply basic security measures before deploying applications. 3 min Read guide](https://streethosting.com.br/en/guides/vps/install-docker-ubuntu-vps)

[← Back to the Guide Center](https://streethosting.com.br/en/guides)
