168

I have .env file at root folder file

NODE_ENV=development
NODE_HOST=localhost
NODE_PORT=4000
NODE_HTTPS=false
DB_HOST=localhost
DB_USERNAME=user
DB_PASSWORD=user

And server.js file in the root/app/config/server.js folder. The first line of server.js file is

require('dotenv').config();

I also tried following:

require('dotenv').config({path: '../.env'});

require('dotenv').config({path: '../../.env'});

However, my env variable are not loaded when I run the server.js file from command prompt

node root/app/config/server.js

If I use the visual studio and press F5, it loads!!

I'm not sure what I'm doing wrong, what I'm missing. Any suggestion is highly appreciate. Thanks.

9
  • 5
    The current working directory might be a hint for you.
    – zerkms
    Feb 20, 2017 at 1:17
  • Hi @zerkms, not sure I follow you. Feb 20, 2017 at 1:19
  • 2
    Ok. What is ../.env? It's a relative path. That is relative to ... what?
    – zerkms
    Feb 20, 2017 at 1:34
  • 2
    Or you could use absolute paths instead and not rely on current working directory value: nodejs.org/docs/latest/api/globals.html#globals_dirname
    – zerkms
    Feb 20, 2017 at 2:02
  • 1
    Unless in your case it's not a require.
    – zerkms
    Feb 20, 2017 at 2:17

28 Answers 28

200

How about use require('dotenv').config({path:__dirname+'/./../../.env'}) ?

Your problem seems to be the execution path.

7
  • 8
    That's not going to work. You are mixing absolute and relative path together. You should use Path.Join instead. Feb 20, 2017 at 2:08
  • 4
    @ZammyPage there is no reason why combining an absolute path and a relative path would not work (if they add the leading slash to the relative path to the env file it must work)
    – zerkms
    Feb 20, 2017 at 2:18
  • Sorry I forgot the leading slash :(. I'm using dotenv on my service like this. It doesn`t work? Feb 20, 2017 at 2:22
  • 6
    require('dotenv').config({ path: `${__dirname}/../../config.env` }) using back-ticks would looks clean
    – KwodKewe
    Nov 12, 2020 at 20:13
  • 4
    How do you allow .env to be overridden by .env.local ? Jan 15, 2021 at 13:27
108

This solved my issues in Node v8.14.1:

const path = require('path')
require('dotenv').config({ path: path.resolve(__dirname, '../.env') })

Simply doing require('dotenv').config({path:__dirname+'/./../../.env'}) resulted in a location that resolved as /some/path/to/env/./../../.env

4
  • Thanks for the resolution and it worked for me. In my case I created one file in Controller folder and tried to output the require('dotenv').config() --> It was showing me "c:\users\..\controller\.env". Really weired, why it is referencing controller folder instead root path. But when tried with Absolute path(Just for temporary workaround) it worked. Aug 12, 2019 at 18:31
  • @ashishsarkar - how does your path string look like? Inside your config method?
    – DavidP
    Aug 14, 2019 at 7:47
  • 1
    @DavidP Finally working solution,does that aproach works well also in production? Feb 8, 2021 at 15:07
  • 2
    @Goran_Ilic_Ilke - assuming your prod have the same structure, then it should.
    – DavidP
    Feb 8, 2021 at 15:13
27

Here is a single-line solution:

require('dotenv').config({ path: require('find-config')('.env') })

This will recurse parent directories until it finds a .env file to use.

You can also alternatively use this module called ckey inspired from one-liner above.

.env file from main directory.

# dotenv sample content
U[email protected]
PASSWORD=iampassword123
API_KEY=1234567890

some js file from sub-directory

const ck = require('ckey');

const userName = ck.USER;     // [email protected]
const password = ck.PASSWORD; // iampassword123
const apiKey   = ck.API_KEY;  // 1234567890
2
  • how do you use this in a .env file? To map it to a js file in a sub-directory? Trying to do this: export GOOGLE_APPLICATION_CREDENTIALS=/path/to/config.json
    – rom
    Dec 11, 2022 at 4:42
  • 2
    Do you need to npm install find-config as I'm getting Error: Cannot find module 'find-config'?
    – Andrew S
    May 25 at 0:49
20

If you are invoking dotenv from a nested file, and your .env file is at the project root, the way you want to connect the dots is via the following:

require('dotenv').config({path:'relative/path/to/your/.env'})
5
  • 10
    AFAIK, dotenv takes only an absolute path and note a relative path.
    – kapad
    Aug 31, 2018 at 11:37
  • 2
    you're right.. but the path is relative to where the process is run from, and not the file in which dotenv is being called. That was what happened when I was debugging this.
    – kapad
    Sep 1, 2018 at 13:01
  • This fixed my problem with "undefined" .env variables. I even have another project running with the same exact setup for configuring server and dotenv and couldn't get it to work without explicitly defining the path. Nov 9, 2020 at 14:48
  • Which "answer above"? Jun 1, 2021 at 10:52
  • There are so many answers, I'm not sure at this point. I'll update the text to reflect this being a potential solution. It's worked for the folks commenting here.
    – zero_cool
    Jun 4, 2021 at 22:55
19

One of the comments in @DavidP's answer notes logging the output of dotenv.config with

console.log(require("dotenv").config())

This will output a log of the config and display errors. In my case it indicated the config method was referencing the current directory instead of the parent directory which contained my .env file. I was able to reference that with the following

require('dotenv').config({path: '../.env'})
2
  • Strange enough, logging it shows the correct data in an object named "parsed" as in { parsed: {} } But, reading any variable using process.env.name still says undefined. May 29, 2022 at 9:12
  • @LalitFauzdar I know this comment is very old but I'd like to respond to add some clarity to that issue for others. If you're using a front-end framework like React or Vue you need to include a prefix to the name of the variable. For React it is "REACT_APP_YOUR_VARIABLE" Vue is "VUE_APP_YOUR_VARIABLE"
    – Digglit
    Sep 11, 2022 at 18:10
14

I've had this problem and it turned out that REACT only loads variables prefixed with REACT_APP_

VueJs can have a similar issue as it expects variables to be prefixed with: VUE_APP_

1
  • wow! good catch
    – ukie
    Feb 16, 2022 at 18:20
10

In the remote case that you arrive till this point, my issue was quite dumber: I wrongly named my env variables with colon ":" instead of equals "=". Rookie mistake but the resulting behavior was not loading the misspelled variables assignment.

# dotenv sample content

# correct assignment
[email protected]

# wrong assignment (will not load env var)
USER : [email protected]
1
  • I did the same thing. Copied from a JSON object and forgot to format it. Thanks for saving me from another 2 hours of banging my head Nov 5, 2021 at 15:08
9

Be sure to load .env at the beginning of the entry file (e.g. index.js or server.js). Sometimes, the order of execution loads the environment variables after the services are initiated. And, by using __dirname, it can easily point to the file required relative to the current file.

Here my project structure is like this.

.
├─ src
│  └─ index.ts
└─ .env
// index.ts
import dotenv from 'dotenv';
import path from 'path';

dotenv.config({path: path.join(__dirname, '..', '.env')});

...
2
  • Not working in Node TS. May 29, 2022 at 9:13
  • 1
    put your dotenv config in another module file and import it when you need
    – LeulAria
    Jun 14, 2022 at 13:41
7

You can first debug by using:

console.log(require('dotenv').config())

In my scenario, my .env file is in root directory and I need to use it in a nested directory. The result gives me:

{
  parsed: {
    DATABASE_URL: 'mongodb://localhost/vidly',
    PORT: '8080'
  }
}

So I simply parse the result and store it in a variable:

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

Then access my DATABASE_URL like a JS object:

dotenv.DATABASE_URL
1
  • Thank you so much; Firstly It showed an error, that expected the env to be in src. But now I could track it with ease
    – testing_22
    Jul 29 at 18:48
6

This solved the issue for me:

const path = require('path');
require('dotenv').config({
  path: path.resolve('config.env'),
});
6

Try this:

const dotenv = require('dotenv');
dotenv.config({ path: process.cwd() + '/config/config.env' });

worked for me idk how??

5

It took me a few head scratches, and the tip to log the output of the require statement to console was really helpful. console.log(require('dotenv').config());

Turns out I was running my app from my user/ directory with nodemon application_name/. and that was making dotenv look for the .env file in my home dir instead of the app's. I was lazy by skipping one cd and that cost me a few minutes.

5
  const path = require('path');
  const dotenv = require('dotenv');
  dotenv.config({ path: path.resolve(__dirname, '../config.env') })
2
  • 4
    Your answer could be improved by adding more information on what the code does and how it helps the OP.
    – Tyler2P
    Jun 12, 2022 at 13:36
  • 1
    It works . You need to import path from "path"; Aug 10, 2022 at 4:01
4

I found an option debug: true to be sent in the config

dotenv.config({ debug: true });

which showed me the following:

[dotenv][DEBUG] "PORT" is already defined in `process.env` and was NOT overwritten

I added overwrite: true and got it working:

[dotenv][DEBUG] "PORT" is already defined in `process.env` and WAS overwritten

I know I might be too late answering, but decided to share my findings after hours of checking the documentation.

2

In my case .env was read fine, but not .env.local.

Updating package.json to name .env into .env.local ( cp ./.env.local .env) solved the problem:

  "myscript": "cp ./.env.local .env && node ./scripts/myscript.js"
2

if config.env file and index.js file both present in the same directory: enter image description here

then, file: index.js

const path = require('path'); 

//  Set port from environment variables
dotenv.config({path: 'config.env'})
const PORT = process.env.PORT || 8080

file: config.env:

PORT = 4000
1

You can need the path of the .env file relative to the current working directory from where the application was launched. You can create this path like this:

const path = require('path')
require('dotenv').config({path: path.relative(process.cwd(), path.join(__dirname,'.env'))});

process.cwd() returns the absolute path of the working directory.
__dirname returns the absolute path of the application.
path.join() adds the path of the .env-file to the path of the application. so if your .env file is nested deeper, just add the folders (e.g. path.join(__dirname, 'config', 'secret','.env'))
path.relative() creates the relative path.

1

Typescript.

if you are trying to resolve __dirname and you are compiling your source folder in another folder, make sure that you edit __dirname. in my case i am compiling ts in dist folder and env files are not located in dist folder, but in the root folder. so you should delete /dist from __dirname. To debug it, you can call error() function which returns error if there is problem with reading env file.

require('dotenv').config({
    path:  __dirname.replace('\dist','') + `${process.env.NODE_ENV}.env`
  }); # To debug .error()

another thing, when you set env variables make sure that following: no space between variable and (&&) as following

  "scripts": {
    "build": "npx tsc",
    "start": "set NODE_ENV=production&& node dist/index.js",
  },
1

I had a problem with the file encoding of the .env file in windows. It was encoded in UTF-16LE. The file was parsed but it was considered empty. After i converted the .env file to UTF-8 everything works as expected.

1

If you are installed dotenv as production dependency. Then, it may not work properly. The .env file is commonly utilized during development when using dotenv to import environment variables into the application's environment. So,

npm install --save-dev dotenv

Not that:

npm install dotenv

To working properly of dotenv library, you always need to mention that code:

import dotenv from "dotenv"
dotenv.config();

In Top of the file like this:

enter image description here

If you put that configuration in the middle. Then, you will see some wired behavior. Like, in some file env variable working file. But, in some, it is not.

0

One time I have got the same problem. Dotenv did not load .env file. I tried to fix this problem with a path config, to put .env file in a root folder, to put .env in the folder where the file is running and nothing helps me. Then I just trashed Node_modules folder, reinstall all dependencies and it works correctly

0

I'am using:

import findUp from 'find-up';
dotenv.config({ path: findUp.sync('.env') });
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 19, 2022 at 6:43
0

What worked for me using Playwright / typescript:

1 create file and place at the root of the project: global-setup.ts add inside:

async function globalSetup() {
  // Configure ENV variables
  require('dotenv').config()
}

export default globalSetup

2 Then use this file into playwright.config.ts

as: globalSetup: require.resolve('./global-setup'),

In this way, global conf is created, and pickup in every single test.

-1

Use only absolute path if you are using some other tool(installed globally) to call your config file somewhere ...

require('dotenv').config({
  path: '/path/to/my/project/.env',
});
-1

One silly mistake I did was i used colon ":" in place of "="

I used below

USER:root

But it should be

USER=root

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Aug 17, 2022 at 15:57
-1

If nothing helps put your .env outside the src folder if you have folder structure like

-src/
  - index.js
  - env.js (where you call dotenv.config)
  // you may call dotenv.config anywhere but entry point is best.

.env (file outside the src folder)
-2

The fastest fix here, just into seconds, add the variables to the platform you are using in my case render.com enter image description here

-3

My failer was the path keyword . should be the P not capital Letter . that was so funny

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.