Read only using nconf and env

I use nconf to handle configuration in my application. The way I configure it is as follows:

nconf.env({ separator: '__', whitelist: ['foo', 'bar'] }) .file('config.json') 

It seems like I cannot change the values ​​if they were obtained through an environment variable. For instance,

 console.log(nconf.get()); // {"foo":123,"bar":356} nconf.set('foo', 789); console.log(nconf.get()); // {"foo":123,"bar":356} 

I checked the stores nconf attribute and seemed to suggest that env variables are read-only?

 console.log(nconf.stores); /** * { env: * { type: 'env', * store: { foo: [Object] }, * mtimes: { 'foo': 1372348332705 }, * readOnly: true, <-- here * loadFrom: null, * whitelist: * ... 

Is it possible to change the variables set with env variables at runtime? If I set the values ​​that were set using the config.json file, I can change the values ​​without any problems.

Any help is much appreciated :-)

+4
source share
1 answer

This is how i solved it

 nconf.stores.env.readOnly = false; nconf.set('foo', 789); nconf.stores.env.readOnly = true; console.log(nconf.get()); // {"foo":789,"bar":356} 

Hope this helps.

0
source

Source: https://habr.com/ru/post/1488529/


All Articles