token is undefined

So, i was just trying to create a bot that can have simple slash commands so i can remind some points that i studied that day and so on. But it gives me the following error when i run node index.js:
TypeError: Cannot destructure property 'TOKEN' of 'process.dotenv' as it is undefined.
TypeError: Cannot destructure property 'TOKEN' of 'process.dotenv' as it is undefined.
The code:
const { Client, Events, GatewayIntentBits } = require('discord.js')
const dotenv = require('dotenv')
dotenv.config()
const { TOKEN, CLIENT_ID, GUILD_ID } = process.dotenv

const client = new Client({ intents: [GatewayIntentBits.Guilds] })

client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`)
});

client.login(TOKEN)
const { Client, Events, GatewayIntentBits } = require('discord.js')
const dotenv = require('dotenv')
dotenv.config()
const { TOKEN, CLIENT_ID, GUILD_ID } = process.dotenv

const client = new Client({ intents: [GatewayIntentBits.Guilds] })

client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`)
});

client.login(TOKEN)
In the .env file, i have the TOKEN copy+paste from the Discord Applications part. I think it may be a simple error but if anyone can help (:
12 Replies
vince
vince•2y ago
Have you tried logging the process.dotenv to see what it gives you?
loss
loss•2y ago
actually no, how can i do that? i'm relearning things now from a big break
vince
vince•2y ago
You should be able to, if process.dotenv is undefined try to just log process. I've never grabbed multiple variables like that admittedly; from what I remember I usually just do something like
const token = process.env.TOKEN;
const token = process.env.TOKEN;
Obviously that's going to get annoying if you have multiple variables like you have, but if you can't get it to work it's a way Ah sorry I didn't read your error message, try just logging process and see if that's undefined too Wait, have you tried just process.env? I don't think process.dotenv is a thing
loss
loss•2y ago
oh wait, really? i was wondering about using only process.envinstead of .dotenv but i was following along with a tutorial that uses .dotenv itself oh vince, thanks again lol now is working
loss
loss•2y ago
in the tutorial is like that and it worked
loss
loss•2y ago
so this made me get a little lost along the way, also is a few months old, so prob is that the issue?
vince
vince•2y ago
Great 🙂 Here's a bit more of a common approach from the dotenv's npm page (https://www.npmjs.com/package/dotenv):
require('dotenv').config()
console.log(process.env) // remove this after you've confirmed it is working
require('dotenv').config()
console.log(process.env) // remove this after you've confirmed it is working
So you don't need to even set the dotenv variable Lol that's why I pretty much never follow youtube tutorials unless I'm really going through it 😂 Docs are much more informative and up to date (usually) but if I'm desperate I'll try YT
loss
loss•2y ago
So i should make my code like:
const { Client, Events, GatewayIntentBits } = require('discord.js')
require('dotenv').config()
const { TOKEN } = process.env;

const client = new Client({ intents: [GatewayIntentBits.Guilds] })

client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`)
});

client.login(TOKEN)
const { Client, Events, GatewayIntentBits } = require('discord.js')
require('dotenv').config()
const { TOKEN } = process.env;

const client = new Client({ intents: [GatewayIntentBits.Guilds] })

client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`)
});

client.login(TOKEN)
? lol is funny how this is working perfect but i got stuck for 20 minutes on that part xD got it, i'll read more about dotenv in the link you send me 🙋
vince
vince•2y ago
Lol yea, that should work, you can have multiple variables destructured too so you can do
const { TOKEN, GUILD_ID, ... } = process.env
const { TOKEN, GUILD_ID, ... } = process.env
I get stuck on stupid stuff all the time don't sweat it everyone does lol, not even your fault anyway the tutorial was just old
loss
loss•2y ago
when following tutorials, i should follow and always read the docs to see if is up to date or only when it gets an error?
vince
vince•2y ago
It's up to you, if I was doing that I would just go until I get an error. The problem is though sometimes things might still work, but the docs might be more up to date and adopt a newer and better approach if that makes sense Tutorials are great to follow along to when you get stuck but it might be better for you to try and read the docs first, usually they have examples to get you up and running relatively quickly. Tutorials on youtube can be out of date in months with how fast tech in the web moves
loss
loss•2y ago
oh got, thanks a lot, vince (:
Want results from more Discord servers?
Add your server