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

Setting environment variable as an object adds properties instead of replacing objects #312

Open
RafGDev opened this issue Nov 28, 2018 · 2 comments

Comments

@RafGDev
Copy link

RafGDev commented Nov 28, 2018

Hi,

Thanks for your work on this project. Just say I have the following files`

config.json:

"database: "{
  "port": 3000
}

index.js

const nconf = require('nconf');

nconf.env({parseValues: true}).file('config.json')
console.log(nconf.get('database'));

When I run database='{"host": "helloworld.com"}' node index.js The database property will return both the port property and the host property. I'd expect my environment variable to completely ovveride and only give me the host property. This is what I get back instead:

{
  port: 3000,
  host: "helloworld.com"
}
@bangerkuwranger
Copy link

nconf considers all hierarchical keys' uniqueness, and thus also the final value of a key's value returned by get(), based on the full chain of keys in the hierarchy. It does not, generally, override a key that is itself an object in its entirety.

In your example, nconf starts by parsing your env arg, and gets a single 'key': database:host
It then adds a file store, which contains another 'key': database:port
This is how it maintains the hierarchy so you can call nconf.get('database:port') instead of having to call nconf.get('database') and then pulling the port value out.

nconf is doing what it is supposed to here; in order for you to override the database:port value on the command line, you'd need to set it explicitly in the database value to the correct value in context of your code. Essentially, nconf considers that 'unset' in the parsed env store, so it returns the value from your file store.

This is how it is supposed to work, so you may need to change the interpretation of these values in your code, or how your config is structured, if you'd like to override the behavior in order to remove port from your database settings if env does not contain it.

@mlarcher
Copy link

mlarcher commented Apr 28, 2020

Is there a way to force the deletion of a part of the config ? I'm running into the same issue in our unit tests, and I would like to override completely some nested values that come from the config file.
Even setting it to undefined or a random string before giving it the intended object value does not do the trick. Any workaround would be nice.

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

3 participants