Dotenv: custom path doesn't work

Created on 30 Dec 2015  路  11Comments  路  Source: motdotla/dotenv

Hi guys

I am trying to move my .env.production file into config dir.
However, it doens't work.

//var dotenv = require('dotenv'); // it works
var dotenv = require('dotenv').config({path: './config/.env.production'}); // doesn't work
dotenv.load();

config dir the same level as app.js

Most helpful comment

I've faced the same issue some weeks ago . . .
and the simplest solution i came up with is this

//  importing resolve from path module 
const {resolve} = require('path')

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

i guess this isn't the prefect answer but it works !

All 11 comments

sorry, kind of missed it in the doc.

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

instead of

var dotenv = require('dotenv').config({path: './config/.env.production'}); // doesn't work

Sorry for the confusion. Trying to make it clearer in #101 that config is an alias to load

@maxbeatty awesome!

I'm sorry but I don't get it...

I'm unsure where I should require, or set it to const dotenv... Could someone please share a working example, I understand that config.() and const dotenv = require can't be on the same line but I don't understand what it should be if not that... ALso I couldn't find anything regarding load...

Thanks in advance.

EDIT :

Okay so I was sure I'd tried it but I came back to it fresh and it's much simpler than I expected :

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

And this can apparently be called multiple times and the values will be overwritten.

EDIT 2 :

Actually that doesn't work...

You can call config many times but process.env will not be overwritten

Arf, indeed, I don't know what I'd had done, I thought it worked but clearly it doesn't...

Then I guess I still don't get it, I have the code I've written above, I do get what I've defined in my root .env file, but I'm not getting the variables I've written in the one I'm explicitly calling with .config()...

What did I miss ?

Here's how to debug and inspect what dotenv is doing:

const dotenv = require("dotenv")

// enable debugging
const { parsed, error } = dotenv.config({ debug: true })

// was there an error?
console.error(error)

// what was parsed?
console.log(parsed)

// compare to process.env
console.dir(process.env)

I've faced the same issue some weeks ago . . .
and the simplest solution i came up with is this

//  importing resolve from path module 
const {resolve} = require('path')

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

i guess this isn't the prefect answer but it works !

I've faced the same issue some weeks ago . . .
and the simplest solution i came up with is this

//  importing resolve from path module 
const {resolve} = require('path')

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

i guess this isn't the prefect answer but it works !

@SiMoG97 worked for me! thanks!!

Just a simple reminder of what have worked for me: You dont need to put the relative path inside pathstring. Just try @maxbeatty code. For whatever dir you have on the root folder.

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

Thanks a lot. It works

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jakobud picture Jakobud  路  3Comments

AndersDJohnson picture AndersDJohnson  路  4Comments

shellscape picture shellscape  路  3Comments

AndrewChen1982 picture AndrewChen1982  路  4Comments

goldbergyoni picture goldbergyoni  路  3Comments