166

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.

miken32
  • 42,008
  • 16
  • 111
  • 154
ANewGuyInTown
  • 5,957
  • 5
  • 33
  • 45
  • 5
    The current working directory might be a hint for you. – zerkms Feb 20 '17 at 01:17
  • Hi @zerkms, not sure I follow you. – ANewGuyInTown Feb 20 '17 at 01:19
  • 2
    Ok. What is `../.env`? It's a relative path. That is relative to ... what? – zerkms Feb 20 '17 at 01:34
  • Sorry, I think I should have done `../../.env` (which I also did btw), relative to `server.js` file, since it is called inside server.js file? – ANewGuyInTown Feb 20 '17 at 01:36
  • @zerkms I think I got what you mean. It should be relative to the directory where I run `node.exe`. so, in that case, I should run my `node.exe` at the root directory. Is that correct? – ANewGuyInTown Feb 20 '17 at 01:46
  • 2
    Or you could use absolute paths instead and not rely on current working directory value: https://nodejs.org/docs/latest/api/globals.html#globals_dirname – zerkms Feb 20 '17 at 02:02
  • I believe all the `require(path)` syntax I see over the internet , uses the `relative path`. I always thought it's relative to the script that's calling it. It seems quite confusing to think in terms of `current node process working directory` because it can be any path. – ANewGuyInTown Feb 20 '17 at 02:06
  • 1
    Unless in your case it's not a `require`. – zerkms Feb 20 '17 at 02:17
  • Yes, you are right. for `require`, the path is relative to the script itself, however from dotenv, it looks like path is relative to the current working directory. Thanks a lot for helping out!!!! – ANewGuyInTown Feb 20 '17 at 02:32

28 Answers28

195

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

Your problem seems to be the execution path.

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
Yonghoon Lee
  • 2,217
  • 1
  • 12
  • 8
  • 8
    That's not going to work. You are mixing absolute and relative path together. You should use Path.Join instead. – ANewGuyInTown Feb 20 '17 at 02: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 '17 at 02:18
  • Sorry I forgot the leading slash :(. I'm using dotenv on my service like this. It doesn`t work? – Yonghoon Lee Feb 20 '17 at 02:22
  • 6
    **require('dotenv').config({ path: \`${__dirname}/../../config.env\` })** using back-ticks would looks clean – KwodKewe Nov 12 '20 at 20:13
  • 4
    How do you allow `.env` to be overridden by `.env.local` ? – Dimitri Kopriwa Jan 15 '21 at 13:27
  • Like @ANewGuyInTown commented: dotenv.config({ path: path.join(__dirname, "..", ".env" }); an example using path.join – Emanuel Aug 15 '21 at 19:21
  • Omg this answer was so helpful! I was trying to use the syntax `require('dotenv').config(__dirname+'/.env')` for the longest time, not realizing an object is required! – Michael Sohnen Nov 02 '22 at 20:13
105

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

DavidP
  • 1,788
  • 1
  • 15
  • 23
  • 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. – ashish sarkar Aug 12 '19 at 18:31
  • @ashishsarkar - how does your path string look like? Inside your `config` method? – DavidP Aug 14 '19 at 07:47
  • 1
    @DavidP Finally working solution,does that aproach works well also in production? – Goran_Ilic_Ilke Feb 08 '21 at 15:07
  • 2
    @Goran_Ilic_Ilke - assuming your prod have the same structure, then it should. – DavidP Feb 08 '21 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
USER=sample@gmail.com
PASSWORD=iampassword123
API_KEY=1234567890

some js file from sub-directory

const ck = require('ckey');

const userName = ck.USER;     // sample@gmail.com
const password = ck.PASSWORD; // iampassword123
const apiKey   = ck.API_KEY;  // 1234567890
Sachi
  • 1,286
  • 1
  • 10
  • 17
  • 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 '22 at 04:42
  • 1
    Do you need to npm install find-config as I'm getting Error: Cannot find module 'find-config'? – Andrew S May 25 '23 at 00: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'})
zero_cool
  • 3,960
  • 5
  • 39
  • 54
  • 10
    AFAIK, dotenv takes only an absolute path and note a relative path. – kapad Aug 31 '18 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 01 '18 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. – Tony Drummond Nov 09 '20 at 14:48
  • Which "answer above"? – Dan Dascalescu Jun 01 '21 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 04 '21 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'})
Digglit
  • 576
  • 1
  • 4
  • 11
  • 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. – Lalit Fauzdar May 29 '22 at 09: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 '22 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_

Greg Pagendam-Turner
  • 2,356
  • 5
  • 32
  • 49
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
USER=sample@gmail.com

# wrong assignment (will not load env var)
USER : sample@gmail.com
dnhyde
  • 1,265
  • 2
  • 14
  • 24
  • 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 – Alex Wohlbruck Nov 05 '21 at 15:08
8

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

...
RileyE
  • 10,874
  • 13
  • 63
  • 106
Arkar Min Tun
  • 605
  • 8
  • 12
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
Malekai
  • 4,765
  • 5
  • 25
  • 60
Jason Huang
  • 179
  • 1
  • 3
  • 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 '23 at 18:48
6

This solved the issue for me:

const path = require('path');
require('dotenv').config({
  path: path.resolve('config.env'),
});
Franco Fontana
  • 115
  • 1
  • 6
6

Try this:

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

worked for me idk how??

Aditya Singh
  • 69
  • 1
  • 2
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') })
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.

Stephane
  • 11,056
  • 9
  • 41
  • 51
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"
Be Kind
  • 4,712
  • 1
  • 38
  • 45
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
vidur punj
  • 5,019
  • 4
  • 46
  • 65
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.

juppic
  • 11
  • 1
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.

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') });
Danubius
  • 59
  • 5
  • 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](/help/how-to-answer). – Community Sep 19 '22 at 06: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.

vlatko606
  • 909
  • 1
  • 13
  • 21
0

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.

DSDmark
  • 1,045
  • 5
  • 11
  • 25
-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',
});
Russo
  • 2,186
  • 2
  • 26
  • 42
-1

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

I used below

USER:root

But it should be

USER=root

  • 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](/help/how-to-answer). – Community Aug 17 '22 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

timjini
  • 55
  • 1
  • 6
-3

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

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33565965) – Gaurav Sharma Jan 06 '23 at 09:35