-
-
Notifications
You must be signed in to change notification settings - Fork 859
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 'replace' option to overwrite existing process.env keys #115
Comments
Can you expand on your use case for needing to overwrite existing environment variables? The current logic is based on the assumption that you have control over the environment you're running your code in meaning you should be able to control the order in which your environment variables are assigned. You can load multiple files to emulate overwriting. .env.override
.env
index.js const assert = require('assert')
const dotenv = require('dotenv')
dotenv.load({ path: '.env.override' })
dotenv.load()
assert.equal(process.env.NODE_ENV, 'override') If |
Hi @maxbeatty Thanks for the fast reply. In my case the environment is coming from the parent process (currently it is supervisord), and I simply want to override any inherited environment with my So the case is that the Anyhow, my suggestion is indeed a tiny addition, that may or may not be useful to anyone else, but it's not very disturbing, right? Many thanks! |
and according to your terminology, this option is best described as |
Sorry, I'm still not totally understanding the use case. I get you want Are there certain environments where this is more useful than others? Testing environments like CI can sometimes be hard to configure thus needing more options. Some quick pseudo code or an example repo would really help 😸 |
one specific case is enabling/disabling a specific service that we run internally. |
Instead of adding an additional configuration option, I think a better approach is to assign your environment variables in the proper order which it sounds like you have control over. If you're dead set on overriding const fs = require('fs')
const dotenv = require('dotenv')
const envConfig = dotenv.parse(fs.readFileSync('.env.override'))
for (var k in envConfig) {
process.env[k] = envConfig[k]
} |
can you explain why is that any better? |
Just wanted to chime in here and say that dotenv not overwriting my existing environment variables (which is what I expected it to do, prior to reading the FAQs), caused a fair amount of confusion for me. I am running an app in an environment that I don't manage, and that environment has a I would prefer to not worry about what variables exist in the environment, and just to specify what I want them to be, for my app. My workaround is to just prefix my variables. Not ideal, but it works to minimize the chance of collisions. |
Close #115. Add example code for overriding process.env to FAQ
Thank you for the snippet. In my case, I have One of our developers has After debugging I googled and arrived at this thread. And I was surprised that I think it does not harm if the package provides a straigtforward option for us to opt-in in such this case. |
Hi, I have a usecase to support the existence of override option in dotenv. Please suggest how to go about this. Instead of manually setting the key/value in process.env, we can write it without dotenv. I think dotenv should have override option, people use it based on their use case. |
hi
the code ignores existing keys if already found in the process.env and does not overwrite them.
for some cases that works fine, however in some cases we keep a .env file which is expected to overwrite existing environment variables even if already present.
so I think an option would be helpful like that -
load({replace: true})
orload({force: true})
what do you think?
would love to submit a PR if that makes sense.
thanks!
The text was updated successfully, but these errors were encountered: