Getting undefined value of .env in nodejs - node.js

I am getting "undefned value of .env in nodejs. my .env file is on root directory.need help
my code for .env file:-
SECRET_KEY=mynameissuky
my app.js file:-
require('dotenv').config();
console.log(process.env.SECRET_KEY);
installed package - npm i dotenv

In the dotenv documentation, it is shown that the .env file is assumed to be in the current directory.
Path Default: path.resolve(process.cwd(), '.env')
process.cwd() method returns the current working directory of the Node.js process.
Specify a custom path if your file containing environment variables is
located elsewhere.
require('dotenv').config({ path: '/custom/path/to/.env' })
In your case, because your env file is located in the root directory you should specify a custom path for the .env file.
For example, my github.env is placed in the root directory and I use dotenv as following:
require('dotenv').config({path:'github.env'});

Related

Why is dotenv config method returning my system environmental variables?

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

Is there a way to use environment variable in another directory in a node.js app

I have a project with folder structure is as follows,
Root > client, server
I want to use the environment variables form the .env file of my Root directory in the /Server/src, But when I try to import it in /Server/src the environment variables are undefined.
Is there a way to use those environment variables in the Server directory?
// from Root directory
const dotenv = require("dotenv");
dotenv.config();
module.exports = {
NODE_ENV: process.env.NODE_ENV,
}
// from Server directory
const { NODE_ENV,} = require("./config");
console.log(NODE_ENV); //value returns undefined
I see what you're trying to do, but unless the file path is "./config" then it won't work. You said it is in the root directory, so likely you are not using the right path. There will need to be at least one ../ to get back out of the server directory and into the root directory to access the file you're exporting from.
That being said, why bother? You should just require dotenv in any file you need to access the environment variables. I don't know what you're saving by using this method.
Pretty much the same amount of code, but less, since you're not creating this extra layer of abstraction:
// from Server directory
require("dotenv").config();
console.log(process.env.NODE_ENV);
From the documentation here: https://www.npmjs.com/package/dotenv
You can specify a custom path in your client folder files to the .env file by going
require('dotenv').config({ path: '/custom/path/to/.env' })
:)

How to access dotenv variables in other files

I'm using the dotEnv package for my Node/Express server. I have the .env file in the root of my project. In the root I have a src folder where I import the package in my app.js file which is the entry point of my server like so: (root/src/app.js)
import dotenv from "dotenv";
dotenv.config();
This works fine and I can access it in the app.js without issue.
But I have a file called config.js in root/src/config that I also want to use the env variables. But when I use something like process.env.DB_HOST I get undefined. How can I use these env variables in this file (and subsequent files)? I managed to get it to work by using
require("dotenv").config({ path: `${__dirname}/../../.env` });
Is there an easier way to access the env variables without having to import the package with the correct path to the env in each file I want to use it in?

dotenv file is not loading environment variables in express application

The .env file is in my root folder, and it contains:
SECRET_KEY = jakjdhsjd38eu89u1djbcasjbey3hug4rg2u
Also in my index.js file (which is also in root) I have used require('dotenv').config().
But still now I can't access process.env.SECRET_KEY.
When I did console.log it returns undefined.
How to overcome it?
you are having extra spaces,change this
SECRET_KEY = jakjdhsjd38eu89u1djbcasjbey3hug4rg2u
to this
SECRET_KEY=jakjdhsjd38eu89u1djbcasjbey3hug4rg2u
source https://www.npmjs.com/package/dotenv

How to run node app from sub-directory with .env variables of that sub-directory?

I have a Node.js App called app.js and its stored in the directory B.
To run this script correctly it needs enviroment variables. These are stored in a file called .env, which is also in directory B.
In my app.js the env-variables are loaded via require("dotenv").config();
and I can then access them with e.g. process.env.SOME_VAR
So if I am currently located in directory B, I can just use node app and my app will execute just fine.
But if I go to the parent directory A and try to run my app via
node ./B/app
it will not execute, because it seems to have no access to the enviroment variables of the .env file.
So, my question is, how can I run my script from its parent folder, if I want to keep the .env file in the same directory?
You could use dotenv to load the the content of the environment variable i.e.
const dotenv = require('dotenv');
// ...
// Then go ahead to load the .env file content into process.env
dotenv.config({ path: '/full/custom/path/to/your/env/vars' });
you can set path using {path : '../.env'}.
require('dotenv').config({path : '../.env'});
Brilliant! it worked for me, I put my .env inside /vars folder
and used this line
require('dotenv').config({path : 'vars/.env'});
Instead of this line
require("dotenv").config();

Resources