Discord.js Token Not Working with .env
I was following a tutorial on securing my token with a .env file and putting my token in the file. I put the token in correctly and referenced it in my code correctly. The issue I am facing is that it only works when I run it within the editor. When I try and run the code through the .run link it it comes out with an error regarding the token. The following is the error I am getting:
Error [TOKEN_INVALID]: An invalid token was provided.
at Client.login (/home/runner/emhhf5cptch/node_modules/discord.js/src/client/Client.js:204:52)
at /home/runner/emhhf5cptch/index.js:125:8
at Script.runInContext (vm.js:131:20)
at Object. (/run_dir/interp.js:156:20)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
[Symbol(code)]: 'TOKEN_INVALID'
}
I don't understand how the code can be working fine within the editor but when I try and run it with the .run link, it doesn't work. Any help to solve this issue would be greatly appreciated.
The only resolution I can come up with is that for some reason, the code is not able to see the .env file because I am not logged in. I'm not sure how to fix this.
Yeah, the .env file will only work in the editor because otherwise that would defeat it's purpose. The .env file is for keeping secrets, and if anyone had your .run URL, everyone could see that token.
@SixBeeps Is there a fix for this?
@dzlandis Do you need the .env in run? I'll say it again, that's a dangerous idea.
@SixBeeps What would you do if you were me?
@dzlandis I'm not entirely sure what you're trying to accomplish by going to the .run site. Is there a reason why you can't just use the editor?
@SixBeeps Yes, I want to keep the bot up 24/7.
@dzlandis For that, use UptimeRobot or mat1's Super Duper Simple Pinger
@SixBeeps I'm using UpTimeRobot at the moment. What is the best way to secure the token?
@dzlandis Hold on, did you direct it to the editor or the .run page?
@SixBeeps the .run page. I'm experimenting with just using express right now but I don't think it will work for longer than an hour. Not sure.