-
-
Notifications
You must be signed in to change notification settings - Fork 862
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
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 🎉 |
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:
UPD: all keys of both env files will be computed (nothing will be lost) |
@maxbeatty
|
@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...! |
Definitely a must-have. If it's a configuration option that defaults to false no default installations of dotenv would be harmed. |
@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 |
Re-opening this issue. We are going to do this. It is more intuitive. |
This is now available in Docs: https://github.com/motdotla/dotenv#override |
Thank you @motdotla! |
I can't seems to override the variables in the .env file require('dotenv').config({ path: './.env',override: false }); but test variable in the .env was not update |
I can't seems to override the variables in the .env file
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 |
TBH, this improvement should have been considered long ago. |
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:
some discussions can be found here facebook/create-react-app#2230 (comment).
What do you think?
The text was updated successfully, but these errors were encountered: