Dotenv: Ability to require/include other .env files ?

Created on 19 Jan 2018  路  8Comments  路  Source: motdotla/dotenv

- root
  - env
    - dev.env
    - prod.env
    - prod.secret.env

I want to saperate some secrets out to another .env file and gitignore it.
How can I tell prod.env to include prod.secret.env ?

Most helpful comment

You can call dotenv twice or however many times you like to set variables from multiple files.

const dotenv = require('dotenv')
dotenv.config({ path: 'prod.env' })
dotenv.config({ path: 'prod.secret.env' })

All 8 comments

You can call dotenv twice or however many times you like to set variables from multiple files.

const dotenv = require('dotenv')
dotenv.config({ path: 'prod.env' })
dotenv.config({ path: 'prod.secret.env' })

But I don't want to change the business logic, any other thoughts?

  • Just let the *.env could require each other

@fritx

But I don't want to change the business logic

Why not?

@zenflow for example, I had a boilerplate of electron+vuejs: https://github.com/fritx/dapp/blob/3e1c9b06c539b46035e2ed5fb082ed2ab0e4f618/package.json#L8

cp env/prod.env app/dist/.env

If I had to modify the business logic, I should change it to be

cat env/prod*.env >  app/dist/.env

which is not good, so that's why I had the question above.

@maxbeatty will you accept a PR if I write this into the docs? (the fact you can include multiple dotenv files)

I think a blog post explaining why (and why not) you might want to do this along with example code would be more effective. There鈥檚 only so much you can put in a README before it starts to work against its purpose. A blog post can expand on an idea, provide examples, and is easier to discover.

I use .env variables consumed by a docker-compose.yml file for my docker environment, so I think I cannot use the suggested solution from maxbeatty more above (https://github.com/motdotla/dotenv/issues/256#issuecomment-359116714).

My .env file contains tons of default variables shared by every environment, plus the "secret" variables to be used in different environments, so I experienced the same "tedious manual process to compare and figure out what need to be added or removed in the other enviroments" (cit: https://www.npmjs.com/package/dotenv-extended)

So like Fritx, I really need so put the secrets out to another .env file and gitignore it, to simplify things also with my CI/CD scripts.

Is there a possibility to add this "require/include" feature in .env files?

Thank you very much for considering this.

dotenv.config({ path: 'prod.env' })
dotenv.config({ path: 'prod.secret.env' })

This doesn't work for me if both have the variables declared and the second one is supposed to override the values of the other one

Edit:
The order should be inversed as once defined it won't look elsewhere, which is a bit controversial
So it works like this

dotenv.config({ path: 'prod.secret.env' })
dotenv.config({ path: 'prod.env' })
Was this page helpful?
0 / 5 - 0 ratings

Related issues

shellscape picture shellscape  路  3Comments

AndersDJohnson picture AndersDJohnson  路  4Comments

awesomejerry picture awesomejerry  路  4Comments

Vandivier picture Vandivier  路  3Comments

goldbergyoni picture goldbergyoni  路  3Comments