-
Notifications
You must be signed in to change notification settings - Fork 65
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
dotenv usage question #25
Comments
This is a really good point, I don't like that I think we can take an even simpler approach though, and maintain the existing const cleanEnv = (inputEnv, config) => {
const dotEnvVars = require('dotenv').parse(dotenvBuffer)
const fullEnv = Object.assign({}, inputEnv, dotEnvVars)
// Rest of validation logic here
} Loading the .env file will add a bit of complexity but I think it's worth it to avoid mutating global state and keeping the API small and tidy. Thoughts? |
That's another way to do it, although I think that the whole
If A few more notes about your example:
|
Sorry about the bugs, I was just sketching something quickly without actually looking at the existing code :) Yes, my example is a breaking change. I think it's worth it though to prevent dumping more stuff on You bring up some good points about making the environment more composable. I'm definitely not against adding a new API that gives this kind of control, I think it's a good idea. I think I'd like to continue calling |
Maybe just add a new option to
Sounds good? |
@ibratoev Yep that sounds good 👍 |
… options.dotEnvPath This is a breaking change because the env vars in the `.env` file will no longer be added to `process.env`. See discussion on #25
@ibratoev Pushed some new commits to master for this (the next release will be 3.0 as this is a breaking change). Note that I changed the new option to |
👍 |
Sounds good, thanks! |
… options.dotEnvPath This is a breaking change because the env vars in the `.env` file will no longer be added to `process.env`. See discussion on af#25
Current State
Currently
dotenv.config
is called inside thecleanEnv
method like that:First, let's check the
dotenv.config
2.0 documentation: "Config will read your .env file, parse the contents, and assign it to process.env."So, what happens is that if you call
cleanEnv({ foo: 'bar' }, spec)
and there is a .env file available this will change yourprocess.env
state. I find this kind of unexpected.Using this library for a while, I feel that the goal of
cleanEnv
is to clean and validate the environment that is passed as the first parameter with the spec provided as the second parameter. I don't really see howdotenv.config
fits.How to fix this? Here are my ideas for the next major version.
Proposal
The current
cleanEnv
method can be renamed tocleanProcessEnv
and the first parameter can be removed. Generally, it can have this simple implementation:cleanEnv
should not call internallydotenv.config()
.As an added benefit, this will be compatible with the latest
dotenv
version.What do you guys think?
The text was updated successfully, but these errors were encountered: