69
votes

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.

12
The current working directory might be a hint for you. - zerkms
Hi @zerkms, not sure I follow you. - ANewGuyInTown
Ok. What is ../.env? It's a relative path. That is relative to ... what? - zerkms
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
Unless in your case it's not a require. - zerkms

12 Answers

105
votes

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

Your problem seems to be the execution path.

44
votes

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

20
votes

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
[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
9
votes

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'})
8
votes

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_

3
votes

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]
2
votes

This solved the issue for me:

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

Try this:

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

worked for me idk how??

1
votes

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"
0
votes

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
votes

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'})
-1
votes

Just add

require("dotenv").config();

to the index of your app, it will locate your .env file, you can then use process.env.YOUR_ENV anywhere in your code