dotenv file is not loading environment variables

146,243

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_

Share:
146,243
ANewGuyInTown
Author by

ANewGuyInTown

...

Updated on January 22, 2022

Comments

  • ANewGuyInTown
    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 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.

  • ANewGuyInTown
    ANewGuyInTown over 7 years
    That's not going to work. You are mixing absolute and relative path together. You should use Path.Join instead.
  • zerkms
    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
    Yonghoon Lee over 7 years
    Sorry I forgot the leading slash :(. I'm using dotenv on my service like this. It doesn`t work?
  • kapad
    kapad over 5 years
    AFAIK, dotenv takes only an absolute path and note a relative path.
  • kapad
    kapad over 5 years
    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.
  • ashish sarkar
    ashish sarkar almost 5 years
    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.
  • DavidP
    DavidP almost 5 years
    @ashishsarkar - how does your path string look like? Inside your config method?
  • Tony Drummond
    Tony Drummond over 3 years
    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.
  • KwodKewe
    KwodKewe over 3 years
    require('dotenv').config({ path: `${__dirname}/../../config.env` }) using back-ticks would looks clean
  • Dimitri Kopriwa
    Dimitri Kopriwa over 3 years
    How do you allow .env to be overridden by .env.local ?
  • Goran_Ilic_Ilke
    Goran_Ilic_Ilke over 3 years
    @DavidP Finally working solution,does that aproach works well also in production?
  • DavidP
    DavidP over 3 years
    @Goran_Ilic_Ilke - assuming your prod have the same structure, then it should.
  • Dan Dascalescu
    Dan Dascalescu almost 3 years
    Which "answer above"?
  • zero_cool
    zero_cool almost 3 years
    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.
  • Emanuel
    Emanuel almost 3 years
    Like @ANewGuyInTown commented: dotenv.config({ path: path.join(__dirname, "..", ".env" }); an example using path.join
  • Alex Wohlbruck
    Alex Wohlbruck over 2 years
    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
  • ukie
    ukie over 2 years
    wow! good catch
  • Felix Orinda
    Felix Orinda about 2 years
    This worked fine
  • Lalit Fauzdar
    Lalit Fauzdar about 2 years
    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
    Lalit Fauzdar about 2 years
    Not working in Node TS.
  • LeulAria
    LeulAria almost 2 years
    put your dotenv config in another module file and import it when you need