Dotenv: Add an option which allows override environment variables

Created on 21 May 2017  路  9Comments  路  Source: motdotla/dotenv

It's a feature request.

I think it would be cool if there will be option override or similar which can allow/disallow overriding of environment variables.
Like this:

require('dotenv').config({
  override: true
});

some discussions can be found here https://github.com/facebookincubator/create-react-app/issues/2230#issuecomment-302921036.
What do you think?

Most helpful comment

@maxbeatty the override flag is a must have. There's no harm in including this feature since the default behavior will be as it currently is. dotenv becomes essentially pointless if you have to pass environment variables in the command line. dotenv should override process.env variables and not export them system wide so I don't see how it could affect node or the system it's running on

All 9 comments

This has been discussed before (sorry I'm too lazy to find old links). It's not a good idea because you could accidentally mess with variables that node or your system depend on. You can always override env vars via the command line.

This is unfortunate in my case. :( create-react-app is binding to the wrong host because HOST is set in my system environment. I've always treated the .env files as me stating this MUST be my environment. It is what it is though, and dotenv is still a useful library.

Hey @ApacheEx and @juanpaco I had the same need and built this functionality out in https://github.com/dschaller/dotenv/tree/overwrite-option#overwrite. Give it a try :D

@maxbeatty the override flag is a must have. There's no harm in including this feature since the default behavior will be as it currently is. dotenv becomes essentially pointless if you have to pass environment variables in the command line. dotenv should override process.env variables and not export them system wide so I don't see how it could affect node or the system it's running on

I've made a PR and done my best to update tests and documentations. #370 :tada:

A primitive review case with dotenv-expand && dotenv

const dotEnv = require("dotenv")
const dotEnvExpand = require("dotenv-expand")

const getDotEnvFiles = (dotEnvPath = `${ROOT_PATH}/.env`) => ([
  `${dotEnvPath}.${NODE_ENV}`,
  `${dotEnvPath}`,
])

const setupDotEnv = (dotEnvFile) => {
  dotEnvExpand(
    dotEnv.config({
      path: dotEnvFile,
    }),
  )
}

getDotEnvFiles().forEach((dotEnvFile) => {
  if (fs.existsSync(`${dotEnvFile}.local`)) {
    setupDotEnv(`${dotEnvFile}.local`)
  }
  if (fs.existsSync(dotEnvFile)) {
    setupDotEnv(dotEnvFile)
  }
})

env files for example:

  • .env.production
    APP_NEEDS_MINIMIZE=true
  • .env.production.local
    APP_NEEDS_MINIMIZE=false

UPD: all keys of both env files will be computed (nothing will be lost)

@maxbeatty
This feature should be implemented as stated below:

  • Load specified env file, and apply all settings that does't present in process.env.
    Optional option to override system variables overrideSystemVariables is appreciated since developers better know which variables they want use or override, default behavior should not override, but if developer need do such thing, good customizable project should give necessary power.
  • If dotenv.config called again with another path to env file library should load variables:

    • if overrideSystemVariables=false then override parameters that was loaded from any env file before but should not touch any system variables.

    • if overrideSystemVariables=true then override any parameters.

@tylerlong's solution works very nicely. I'd love to see it brought into core, it's optional after all. It could even have a warning in the documentation...!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shellscape picture shellscape  路  3Comments

DesignByOnyx picture DesignByOnyx  路  4Comments

shai32 picture shai32  路  5Comments

marcusradell picture marcusradell  路  3Comments

shellscape picture shellscape  路  3Comments