I'm building bot with discord.js. Everything was working just fine, but when I run code today I got error that TOKEN that I load from .env file is invalid. I checked it and TOKEN is "undefined". My folder tree look like it:
I'm loading variables with:
const TOKEN = process.env.TOKEN;
In .env file I assign TOKEN like it.
TOKEN=my-bot-token
Any ideas? I was restarting my code editor (VS Code) few times. Exactly the same code worked some time ago...
Thanks!
const TOKEN = process.env.TOKEN;
does not load anything from your .env. You need to use something like dotenv:
const { parsed, error } = require('dotenv').config();
if (error) {
// Handle error
throw error;
}
console.log(parsed);
Also notice:
As early as possible in your application, require and configure dotenv.
Edit: You can also set the path to your .env:
const { parsed, error } = require('dotenv').config({
path: '/full/custom/path/to/your/env/vars'
});