Can't use my .env file!

I just can’t reference my Discord Bot token into my main js file!

Inside .env this is what I have:

TOKEN=NzI2NDMTg4O... (Rest of the token)

and I’m trying to log my bot with this:

client.login(process.env.TOKEN);

I got this packages: (Don’t know which is necessary so added them all)

"dotenv": "^8.2.0",
"dotenv-safe": "^8.1.0",
"dotenv-webpack": "^1.8.0",
"dotenv-flow": "^3.2.0"

How can I fix it?

Hi there @AylanJ123!
Welcome to the Glitch Community! Can you let us know what error you get? This will help us determine the problem.
Eddie

Just to let you know, glitch already has environment variable support meaning you don’t need to add any packages. You should already have a env file where you can add variables using the easy UI Glitch provides

As my bot starts automatically with just visiting the web page or edit page, I get nothing, it will simply not login in Discord. BUT, if I go to the console and write: NOPE

OH I just realized that if I start it up in the console, will successfully log in, if I don’t, it won’t!

Then a better question would be, how to make visitors to the web page start the bot automatically?

Can I see your full package.json file?

{
“name”: “tk-game-bot”,
“version”: “1.0.3”,
“description”: “The Kidnapper Game Bot”,
“main”: “server.js”,
“scripts”: {
“test”: “echo “Error: no test specified” && exit 1”,
“start”: “node watch.json”
},
“keywords”: ,
“author”: “”,
“license”: “MIT”,
“dependencies”: {
“discord.js”: “^12.2.0”,
“np”: “^6.2.5”,
“dotenv”: “^8.2.0”,
“dotenv-safe”: “^8.1.0”,
“dotenv-webpack”: “^1.8.0”,
“dotenv-flow”: “^3.2.0”
}
}

Do you have a watch.json file. If so, can I see that?

As I said, you can remove the dotenv packages as glitch has native support for env files :slight_smile:

1 Like

I’m taking them out soon, I’m new at coding, many guides recommended this code but I couldn’t realize what it exactly does

{
“install”: {
“include”: ["^package\.json$", “^\.env$”]
},
“restart”: {
“exclude”: ["^public/", “^dist/”],
“include”: ["\.js$", “\.json”]
},
“throttle”: 900000
}

Great!
I don’t think you need the watch.json file as glitch has support for most of those things. Can you replace your package.json with this:

{
“name”: “tk-game-bot”,
“version”: “1.0.3”,
“description”: “The Kidnapper Game Bot”,
“main”: “server.js”,
“scripts”: {
“test”: “echo “Error: no test specified” && exit 1”,
“start”: “node server.js”
},
“keywords”: ,
“author”: “”,
“license”: “MIT”,
“dependencies”: {
“discord.js”: “^12.2.0”,
“np”: “^6.2.5”,
“dotenv”: “^8.2.0”,
“dotenv-safe”: “^8.1.0”,
“dotenv-webpack”: “^1.8.0”,
“dotenv-flow”: “^3.2.0”
}
}
2 Likes

This code is assuming that your main file is called server.js

Oh didn’t realize it wasn’t there anymore! btw sometimes it seems that this line of code tends to just disappear

“start”: “node server.js”

Is that normal? is because formatting the code?

It shouldn’t do that. Does the code work now you have replaced the package.json?

1 Like

It is working now! THX!!!

That’s great! Have a nice day! :slight_smile:
Eddie

1 Like