dotenv file is not loading environment variables
Solution 1
How about use require('dotenv').config({path:__dirname+'/./../../.env'})
?
Your problem seems to be the execution path.
Solution 2
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
Solution 3
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
Solution 4
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'})
Solution 5
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_
Comments
-
ANewGuyInTown over 2 years
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 theroot/app/config/server.js
folder. The first line ofserver.js
file isrequire('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 promptnode 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.
-
ANewGuyInTown over 7 yearsThat's not going to work. You are mixing absolute and relative path together. You should use Path.Join instead.
-
zerkms over 7 years@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)
-
Yonghoon Lee over 7 yearsSorry I forgot the leading slash :(. I'm using dotenv on my service like this. It doesn`t work?
-
kapad over 5 yearsAFAIK, dotenv takes only an absolute path and note a relative path.
-
kapad over 5 yearsyou'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.
-
ashish sarkar almost 5 yearsThanks 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.
-
DavidP almost 5 years@ashishsarkar - how does your path string look like? Inside your
config
method? -
Tony Drummond over 3 yearsThis 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.
-
KwodKewe over 3 yearsrequire('dotenv').config({ path: `${__dirname}/../../config.env` }) using back-ticks would looks clean
-
Dimitri Kopriwa over 3 yearsHow do you allow
.env
to be overridden by.env.local
? -
Goran_Ilic_Ilke over 3 years@DavidP Finally working solution,does that aproach works well also in production?
-
DavidP over 3 years@Goran_Ilic_Ilke - assuming your prod have the same structure, then it should.
-
Dan Dascalescu almost 3 yearsWhich "answer above"?
-
zero_cool almost 3 yearsThere 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.
-
Emanuel almost 3 yearsLike @ANewGuyInTown commented: dotenv.config({ path: path.join(__dirname, "..", ".env" }); an example using path.join
-
Alex Wohlbruck over 2 yearsI 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
-
ukie over 2 yearswow! good catch
-
Felix Orinda about 2 yearsThis worked fine
-
Lalit Fauzdar about 2 yearsStrange enough, logging it shows the correct data in an object named "parsed" as in
{ parsed: {} }
But, reading any variable usingprocess.env.name
still says undefined. -
Lalit Fauzdar about 2 yearsNot working in Node TS.
-
LeulAria almost 2 yearsput your dotenv config in another module file and import it when you need