I can't find the problem with this i never made a javascript discord bot before?

Viewed 38

I never made a javascript discord bot and I wanted to make one, so i found a recent good looking tutorial to learn the bascics, but I can't make it work.?

import dotenv from "dotenv";
require("dotenv").config();

import { Client, 
         GatewayIntentBits, 
         ButtonBuilder, 
         ButtonStyle, 
         ModalBuilder, 
         TextInputBuilder, 
         TextInputStyle } from "discord.js";

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.DirectMessages
    ],
});



client.login(process.env.TOKEN);

I ran these commands before:

touch index.js
npm install discord.js
npm i dotenv
touch .env

then i put this in the .env file: TOKEN = "discord bot token"

ERROR:

import dotenv from "dotenv";
^^^^^^

SyntaxError: Cannot use import statement outside a module
2 Answers

At Javascript:

require('dotenv').config()

More info at library doc.

Completing @paulo-eduardo's answer, if you want to use import and export, you need to either use a bundler, or a node version that supports this syntax.

Here, you are mixing require and import/export syntaxes.

Given the error you have, you should only use the require syntax.

Meaning, the final code should result in:

require("dotenv").config();

const { Client, 
         GatewayIntentBits, 
         ButtonBuilder, 
         ButtonStyle, 
         ModalBuilder, 
         TextInputBuilder, 
         TextInputStyle } = require("discord.js");

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.DirectMessages
    ],
});

client.login(process.env.TOKEN);
Related