Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom path doesn't work #116

Closed
kamoljan opened this issue Dec 30, 2015 · 12 comments
Closed

custom path doesn't work #116

kamoljan opened this issue Dec 30, 2015 · 12 comments

Comments

@kamoljan
Copy link

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

@kamoljan
Copy link
Author

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

@maxbeatty
Copy link
Contributor

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

@kamoljan
Copy link
Author

kamoljan commented Jan 3, 2016

@maxbeatty awesome!

@justinmarsan
Copy link

justinmarsan commented Jan 18, 2019

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

@maxbeatty
Copy link
Contributor

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

@justinmarsan
Copy link

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 ?

@maxbeatty
Copy link
Contributor

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)

@SiMoG97
Copy link

SiMoG97 commented Jan 18, 2020

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 !

@camgrafiman
Copy link

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!!

@hqMoura
Copy link

hqMoura commented Apr 13, 2020

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

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

@ghost
Copy link

ghost commented Oct 30, 2020

const {resolve} = require('path')

Thanks a lot. It works

@uwDavid
Copy link

uwDavid commented Mar 30, 2021

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 !

This helped me in finding the .env file 1 level up.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants