# Deploy a Discord.js Bot

> Step-by-step guide to deploying a Discord.js (Node.js) bot on Daki Hosting.

This guide walks you through deploying a Discord.js bot on Daki Hosting — from creating your server to having your bot online and reading its token securely.

## What you'll learn

- How to create a Node.js server for your bot
- How to upload your code
- How to configure the install command, startup command, and Docker image
- How to set your bot token securely using a `.env` file
- How to start your bot and verify it works

---

## Prerequisites

- A Daki account (Free Tier or paid) — see [Create an Account](https://daki.cc/wiki/guides/account-create)
- A Discord bot with a **bot token**, created in the [Discord Developer Portal](https://discord.com/developers/applications)
- Your bot's source code, including a `package.json` with `discord.js` and `dotenv` as dependencies

> **Info:** Discord bots connect **outbound** to Discord's servers and do not require an inbound port. This means Discord bots work on both Free Tier and paid plans. If your bot also serves HTTP requests (e.g., for slash command interactions or webhooks), you will need a paid plan, as publicly accessible ports are only available on paid plans. See [Network & Allocations](https://daki.cc/wiki/guides/network-allocations).

---

## Deployment

**Step 1: Create a Node.js server**

1. Log in to the Free Tier Dashboard at [dash.daki.cc](https://dash.daki.cc) or the Billing Panel at [billing.daki.cc](https://billing.daki.cc).
2. Create a new server and select the **Node.js** egg.
3. Give your server a name (e.g., `MyDiscordBot`).

See [Create Your First Server](https://daki.cc/wiki/guides/create-server) for the full walkthrough.

**Step 2: Access the Portal**

Log in to the Portal via SSO (from the Credentials tab or your Billing Panel service page), or directly at [portal.daki.cc](https://portal.daki.cc). See [Portal Overview](https://daki.cc/wiki/guides/portal).

**Step 3: Upload your bot's code**

**Option A — Upload as a ZIP (simplest):**
1. Compress your project folder into a `.zip` file locally.
2. In the Portal, go to the **Files** tab and click **Upload**.
3. Right-click the `.zip` and click **Extract**.

**Option B — Use SFTP (no size limits):**
Connect using your SFTP credentials from the **Settings** tab and upload your folder directly. See [File Manager](https://daki.cc/wiki/guides/file-manager).

> **Info:** Do **not** include your `node_modules` folder in the upload — it will be installed automatically by the install command. Do include your `package.json` and `package-lock.json`.

**Step 4: Configure the Startup tab**

Open the **Startup** tab and configure all three fields:

**1. Docker Image** — Select the Node.js version your bot requires (e.g., **Node.js 20** or **Node.js 22**). Node.js 20 LTS is a safe default if unsure.

**2. Install Command** — Set this to install your dependencies automatically:
```
npm install
```
**3. Startup Command** — Set this to the command that launches your bot:
- `node index.js` — if your entry file is `index.js`
- `node bot.js` — if your entry file is `bot.js`
- `npm start` — if you have a `start` script in `package.json`

You are free to adjust these commands to match your project structure.

See [Startup Settings](https://daki.cc/wiki/guides/startup-settings) for a full explanation.

**Step 5: Add your bot token via a .env file**

Your bot needs its Discord token to connect. Store it in a `.env` file on the server so it never ends up in your source code.

1. In the **Files** tab, click **New File** and name it `.env`.
2. Add your token:
   ```
   BOT_TOKEN=your_discord_bot_token_here
   ```
3. Click **Save**.

In your bot code, load it with `dotenv`:
```js
require('dotenv').config();

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.once('ready', () => {
    console.log(`Logged in as ${client.user.tag}`);
});

client.login(process.env.BOT_TOKEN);
```
Make sure `dotenv` is listed in your `package.json` dependencies so it gets installed by the install command.

See [Environment Variables](https://daki.cc/wiki/guides/environment-variables) for more details on using `.env` files.

**Step 6: Start your bot**

Click **Start** in the **Console** tab. A successful launch will print your bot's login confirmation:

```

Logged in as MyBot#1234!

```
If you see errors instead, check the [Troubleshooting](https://daki.cc/wiki/guides/troubleshooting) guide.

---

## Common Issues

**Error: Cannot find module 'discord.js'**

Your dependencies were not installed. Check that your `package.json` lists `discord.js` as a dependency, and that the install command is set to `npm install` in the Startup tab. Restart the server to trigger the install.

**Error: Cannot find module 'dotenv'**

Add `dotenv` to your `package.json` dependencies and restart the server to reinstall.

**Error: An invalid token was provided**

Your bot token in the `.env` file is incorrect or missing. Go to the Files tab, open `.env`, and verify the value. Make sure there are no extra spaces, quotes, or line breaks.

**Bot starts but disconnects shortly after**

This is usually caused by missing Gateway Intents. In the Discord Developer Portal, enable the required privileged intents (e.g., Message Content Intent) for your bot application, and ensure your code requests them.

**Server crashes immediately on start**

Open the Console tab and read the error. Common causes: wrong startup command (filename typo), wrong Node.js version, or missing `node_modules`. See [Troubleshooting](https://daki.cc/wiki/guides/troubleshooting).

---

## Next Steps

- [Environment Variables](https://daki.cc/wiki/guides/environment-variables) — Full guide on `.env` files and reading variables in code.
- [Keep Your Free Server Active](https://daki.cc/wiki/guides/save-free-server-from-purge) — Watch ads to keep your Free Tier server alive.
- [File Manager](https://daki.cc/wiki/guides/file-manager) — Update your code files after making changes.

---
Source: https://daki.cc/wiki/guides/deploy-discordjs
