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

Add an option which allows override environment variables #199

Closed
ApacheEx opened this issue May 21, 2017 · 18 comments
Closed

Add an option which allows override environment variables #199

ApacheEx opened this issue May 21, 2017 · 18 comments

Comments

@ApacheEx
Copy link

ApacheEx commented May 21, 2017

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 facebook/create-react-app#2230 (comment).
What do you think?

@maxbeatty
Copy link
Contributor

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.

@juanpaco
Copy link

juanpaco commented Jun 1, 2017

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.

@dschaller
Copy link

dschaller commented Oct 16, 2017

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

@miguelmota
Copy link

@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

@alexewerlof
Copy link

alexewerlof commented Jan 31, 2019

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

@tylerlong
Copy link

tylerlong commented Feb 10, 2019

@furryablack
Copy link

furryablack commented Jul 6, 2019

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)

@GitStorageOne
Copy link

GitStorageOne commented Jul 9, 2019

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

@MaffooBristol
Copy link

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

@berkus
Copy link

berkus commented Jun 3, 2021

Definitely a must-have. If it's a configuration option that defaults to false no default installations of dotenv would be harmed.

@bendavis78
Copy link

@maxbeatty with respect, I completely disagree. The whole point of .env files is to override system environment variables. What if I want to override a node environment variable in my .env file? As the developer I should have the freedom to do this. This is the default behavior with most implementations of dotenv in other languages (see: python).

@motdotla
Copy link
Owner

Re-opening this issue. We are going to do this. It is more intuitive.

@motdotla
Copy link
Owner

This is now available in v14.1.0 and above 🎉: https://github.com/motdotla/dotenv/blob/master/CHANGELOG.md#1410-2022-01-17

Docs: https://github.com/motdotla/dotenv#override

@MaffooBristol
Copy link

Thank you @motdotla!

@TerryGoh
Copy link

TerryGoh commented Mar 9, 2023

I can't seems to override the variables in the .env file
my codes :

require('dotenv').config({ path: './.env',override: false });
let test = process.env.test
process.env.test = "r34343"

but test variable in the .env was not update

@TerryGoh
Copy link

TerryGoh commented Mar 9, 2023

I can't seems to override the variables in the .env file
my codes :

require('dotenv').config({ path: './.env',override: true});
let test = process.env.test
process.env.test = "r34343"

But the test variable in the .env was not update. Any idea how to update variables in the .env file via in js.

@berkus
Copy link

berkus commented Mar 14, 2023

But the test variable in the .env was not update. Any idea how to update variables in the .env file via in js.

It works the other way around, variables defined in .env override whatever was in the environment previously. dotenv doesn't write to any files.

@atljoseph
Copy link

atljoseph commented Apr 7, 2023

TBH, this improvement should have been considered long ago.
Actually found why mine wasn't working, will open up another issue, with dotenv-expand this time.
For some reason it breaks when you redefine a variable with same name, which is pretty crazy.

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