0

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

sandy gill
  • 1
  • 1
  • 1
  • Maybe this will help with your problem: https://stackoverflow.com/questions/42335016/dotenv-file-is-not-loading-environment-variables – m-s7 May 10 '22 at 10:45

1 Answers1

1

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'});