# How to Create a Discord Bot with Node.js and discord.js (Beginner Guide)

Discord bots are a great way to automate tasks, moderate servers, and enhance user engagement. Whether you want a bot for moderation, games, music, or other utilities, creating a bot is simpler than it may seem. This guide will walk you through the s...

Tags: nodejs, javascript
Published: 2025-02-24 (updated 2026-10-22)


Discord bots are a great way to automate tasks, moderate servers, and enhance user engagement. Whether you want a bot for moderation, games, music, or other utilities, creating a bot is simpler than it may seem. This guide will walk you through the steps of building your own Discord bot using **Node.js** and **Discord.js**. If you'd rather go the TypeScript route, I have a follow-up walkthrough on [how to build a Discord bot with Bun and TypeScript](/blog/build-discord-bot-bun-typescript-2026/).

## Prerequisites

Before we begin, ensure you have the following:

* **Node.js** installed on your computer. You can download it from [nodejs.org](https://nodejs.org/).
    
* **A Discord account** and a server where you have admin privileges.
    
* **Basic JavaScript knowledge** to understand the coding aspect.
    

## Step 1: Create a New Discord Application

1. Go to the Discord Developer Portal.
    
2. Click on **New Application** and give your bot a name.
    
3. Navigate to the **Bot** tab on the left panel.
    
4. Click **Add Bot**, then customize its name and profile picture.
    
5. Under "Token", click "Reset Token" and copy it. This is important as it allows your bot to authenticate itself.
    

## Step 2: Set Up Your Project

1. Open a terminal and create a new folder for your bot project:
    
    ```bash
    mkdir discord-bot
    cd discord-bot
    ```
    
2. Initialize a new Node.js project:
    
    ```bash
    npm init -y
    ```
    
3. Install the required dependencies:
    
    ```bash
    npm install discord.js dotenv
    ```
    

## Step 3: Create the Bot Script

1. Create a new file named `index.js` in your project folder.
    
2. Open the file in a code editor (e.g., VS Code) and paste the following code:
    
    ```javascript
    require('dotenv').config();
    const { Client, GatewayIntentBits } = require('discord.js');
    
    const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
    
    client.once('ready', () => {
        console.log(`Logged in as ${client.user.tag}!`);
    });
    
    client.on('messageCreate', message => {
        if (message.content === '!hello') {
            message.channel.send('Hello, world!');
        }
    });
    
    client.login(process.env.TOKEN);
    ```
    

## Step 4: Store Your Token Securely

1. Create a `.env` file in the root folder and add:
    
    ```plaintext
    TOKEN=your-bot-token-here
    ```
    
2. Update your `package.json` to include:
    
    ```json
    "scripts": {
        "start": "node index.js"
    }
    ```
    

## Step 5: Invite Your Bot to a Server

1. Go back to the Discord Developer Portal.
    
2. Navigate to the **OAuth2** tab and select **URL Generator**.
    
3. Choose **bot** as a scope and select necessary permissions (e.g., `Send Messages`).
    
4. Copy the generated link and paste it into your browser to invite the bot to your server.
    

## Step 6: Run Your Bot

In the terminal, start your bot with:

```bash
npm start
```

You should see `Logged in as [bot name]!` in the console, meaning your bot is live.

## Conclusion

You have successfully created a basic Discord bot! You can expand it by adding more commands, integrating APIs, or even making it a fully functional assistant. Explore the Discord.js documentation for more advanced features, and if you want to pair this with something user-facing on the backend, try my walkthrough on [how to build a referral program with Node.js](/blog/how-to-build-a-referral-program-with-nodejs/).
