nconf icon indicating copy to clipboard operation
nconf copied to clipboard

.set() should always work

Open brandon-collins opened this issue 9 years ago • 13 comments

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());

brandon-collins avatar Dec 16 '15 12:12 brandon-collins

Just run into this as well when trying to normalise values from strings coming in from process.env (e.g. to booleans or numbers).

yogaraptor avatar Feb 01 '16 13:02 yogaraptor

+1

cttttt avatar Feb 19 '16 20:02 cttttt

+1

ahules avatar Mar 03 '16 12:03 ahules

Very confusing. +1

julbra avatar Apr 04 '16 13:04 julbra

+1

tkrugg avatar May 05 '16 12:05 tkrugg

I just lost 1/2 hour trying to understand why .set wasn't working. Thank you google for digging up these precious issues!

paulovieira avatar Jun 09 '16 16:06 paulovieira

+1

CenterStoneOSD avatar Jun 29 '16 00:06 CenterStoneOSD

+1

ygotthilf avatar Oct 05 '16 15:10 ygotthilf

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 avatar Oct 05 '16 19:10 brandon-collins

@brandon-collins did you have some time to look into this? Can we help somehow?

palmerabollo avatar Dec 02 '16 16:12 palmerabollo

+1

LeoAref avatar Dec 28 '16 14:12 LeoAref

+1

senseysensor avatar Oct 18 '17 06:10 senseysensor

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.

jtyers avatar Jul 21 '19 07:07 jtyers