I'm new to discord.js and I'd like to learn about the .env file

I've been watching videos on YouTube to get a grasp on what I need to do initially. I've already set up my package.json and my package-lock.json. I'm working on my script.js, but what I really want to figure out is how to use the .env file. I've noticed in several videos that YouTubers use multiple files for each command, including a .env file. And I would also like to understand why my script.js slash command does not appear. npm list discord.js - └── (empty) node -v - v20.11.1
No description
7 Replies
d.js toolkit
d.js toolkit•9mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
d.js docs
d.js docs•9mo ago
:guide: Home: Introduction read more
gex
gex•9mo ago
discord.js - npm install discord.js package.json - npm init:
{
"name": "programming-things",
"version": "1.0.0",
"description": "A JavaScript BOT for Discord.",
"main": "index.js",
"scripts": {
"test": "node ."
},
"keywords": [
"Bot"
],
"author": "gamingexpert",
"license": "ISC",
"dependencies": {
"discord-gamecord": "^4.4.1",
"discord.js": "^14.14.1"
}
}
{
"name": "programming-things",
"version": "1.0.0",
"description": "A JavaScript BOT for Discord.",
"main": "index.js",
"scripts": {
"test": "node ."
},
"keywords": [
"Bot"
],
"author": "gamingexpert",
"license": "ISC",
"dependencies": {
"discord-gamecord": "^4.4.1",
"discord.js": "^14.14.1"
}
}
gex
gex•9mo ago
I didn't understand. If I do this can I create several files and run them all in a .env file? Like this: https://i.imgur.com/gDYxANM.png
Imgur
gex
gex•9mo ago
Yes, I think I did that. But is it used to run codes? I'm not saying that the codes will be inside .env, but the command files like:
ban.js
kick.js
etc...
ban.js
kick.js
etc...
In each of the commands will I have to put something like client.login(/.env) (example)? Ohhh Understood. So, files like kick.js, ban.js, clear.js, help.js... Would I have to get them all to run? I read. But I didn't find where that part was.
gex
gex•9mo ago
No description
gex
gex•9mo ago
Oh, so my script.js is where the Client will be? Is it necessary to use config.json? I'm going to delete everything and start from the beginning. The first thing I'm going to do is package.json. Yes. In the .env file, does the Token need to be enclosed in quotation marks?
const { Client, Events, GatewayIntentBits } = require('discord.js');
const { DISCORD_TOKEN } = require('./.env');

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

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

client.login(DISCORD_TOKEN)
const { Client, Events, GatewayIntentBits } = require('discord.js');
const { DISCORD_TOKEN } = require('./.env');

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

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

client.login(DISCORD_TOKEN)
Note: Using the "preview" debug extension
"...\nodejs\node.exe" ".\...\index.js"
- Process exited with code 1
- Uncaught Error Error: Cannot find module './.env'
- Require stack:
- ...\index.js
- at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
- at Module._load (node:internal/modules/cjs/loader:985:27)
- at Module.require (node:internal/modules/cjs/loader:1235:19)
- at require (node:internal/modules/helpers:176:18)
- at <anonymous> (file:///.../index.js:2:27)
- at Module._compile (node:internal/modules/cjs/loader:1376:14)
- at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
- at Module.load (node:internal/modules/cjs/loader:1207:32)
- at Module._load (node:internal/modules/cjs/loader:1023:12)
- at executeUserEntryPoint (node:internal/modules/run_main:135:12)
- at <anonymous> (node:internal/main/run_main_module:28:49)
Note: Using the "preview" debug extension
"...\nodejs\node.exe" ".\...\index.js"
- Process exited with code 1
- Uncaught Error Error: Cannot find module './.env'
- Require stack:
- ...\index.js
- at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
- at Module._load (node:internal/modules/cjs/loader:985:27)
- at Module.require (node:internal/modules/cjs/loader:1235:19)
- at require (node:internal/modules/helpers:176:18)
- at <anonymous> (file:///.../index.js:2:27)
- at Module._compile (node:internal/modules/cjs/loader:1376:14)
- at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
- at Module.load (node:internal/modules/cjs/loader:1207:32)
- at Module._load (node:internal/modules/cjs/loader:1023:12)
- at executeUserEntryPoint (node:internal/modules/run_main:135:12)
- at <anonymous> (node:internal/main/run_main_module:28:49)
I'm going to use config.json. Nice. It's working.
Want results from more Discord servers?
Add your server