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

1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

issue not resolved. getting undefined value again
@sandygill make sure you write the right path
yes, I added right path
please, move the .env file to the root directory and check this require('dotenv').config({path:'github.env'});
-2

Try this one

const dotenv = require('dotenv').config();

You will get your environment variable for sure.

1 Comment

What does this add over the existing answer from 2022?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.