nconf
nconf copied to clipboard
.set() should always work
I would expect nconf.set() to always work regardless of whether a config file was loaded. I would consider this a bug since it is not documented and is not an obvious behavior given the assumption that set is supposed to set something. other than this painful discovery I love the module, thanks!
var nconf = require('nconf');
// this does not work
nconf
.overrides({ one: 'one'});
nconf.set('two', 'two');
console.log(nconf.get());
// this does work
nconf.file({file: __dirname + '/config.json'});
nconf.set('two', 'two');
console.log(nconf.get());
Just run into this as well when trying to normalise values from strings coming in from process.env (e.g. to booleans or numbers).
+1
+1
Very confusing. +1
+1
I just lost 1/2 hour trying to understand why .set wasn't working. Thank you google for digging up these precious issues!
+1
+1
I might take a stab at fixing this and issue a pr when I get some free time... hopefully in the next couple of weeks.
@brandon-collins did you have some time to look into this? Can we help somehow?
+1
+1
Yup, me too. Quite frustrating!
I've found however that added the memory store appears to work:
const assert = require('assert');
const nconf = require('nconf')
.use('memory')
.env()
assert(nconf.get('PATH')); // outputs your existing PATH from environment
nconf.set('PATH', 'bar'); // set to a bogus value
assert.equal(nconf.get('PATH'), 'bar'); // bogus value persists
Note that the use('memory') must come BEFORE env(), otherwise the set doesn't take effect and the final assert fails. I haven't tested with other configurations but it may be that adding a memory store does in fact fix this issue in all cases.
At the very least it would be better if an exception is thrown if set is called and there aren't any stores that support it.