I am trying to import strings from my .env file located in the root directory of my project into my index.js file. The dotenv config method is importing in all my system environmental variables instead.
Here is the structure of the project folder.
myapp/
.env
package.json
src/
index.js
commands/
I tried running my app out of different directories in the project folder thinking that config was using the cwd to check for the .env file. But neither worked. I have also tried setting the config() argument to "../index.env" because it is the path relative to index.js.
import { config } from "dotenv";
config();
const TOKEN = process.env.DISCORD_TOKEN;
Resulted in bringing in all the env variables like "PATH", "SHELL" etc
CodePudding user response:
Try it like this:
import dotenv from "dotenv";
dotenv.config();
const TOKEN = process.env.DISCORD_TOKEN;
If you have placed the .env file in a different directory, you can specify the path to the file using the path option of the config() function:
dotenv.config({ path: "./../.env" }); // Move to parent folder from the src folder