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.
process.cwd()
method returns the current working directory of the Node.js process.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'});