EzDevInfo.com

nconf

Hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging. indexzero/nconf · GitHub nconf - hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging.

Capturing command line arguments using NCONF

I have a simple node.js backend script and I want to capture command line arguments along with the keys/values from a config.json file and environment variables. The second two I am having no problem with, but I am having nearly inexplicable trouble capturing the command line args.

I can capture the command line arguments this way:

var nconf = require('nconf');
nconf.argv().env().file({file: './config.json'});

var csvFilePath = nconf.argv().get()._[0]; // var csvFilePath = process.argv[2];
var csvType = nconf.argv().get()._[1];     // var csvType = process.argv[3];

these two calls are equivalent to process.argv[index], except the index is changed.

There has to be a more straightforward way to capture the command line arguments but even when I debug and look through the variables that nconf yields, I still can't figure it out.

Anyone with nconf experience care to help?


Source: (StackOverflow)

Using nconf to switch bunyan's log.debug on in dev env while off in production

I'm using nconf in my node js app to segregate config between different environments. At presently I'm using debug module to output certain values to console during development. I have started incorporating bunyan as a logger and I want to use its log.debug in lieu of debug statement to output to console while using nconf to switch it off when in production. This becomes even more useful when using Mocha in TDD fashion where I would like to switch off the log.debug statements polluting the console and interspersing with Mocha output.

I hoping there is a way of switching those log.debug stmt using nconf....I would not like to use another module...need to keep external modules to a minimum.

PS : I have gone through the following stackoverflow post Recommended way of applying Bunyan to a large node application? . While it does have some good suggestion....it does not provide a solution to my query.


Source: (StackOverflow)

Advertisements

nconf is not working with NPM start command

I have these two node.js command line commands:

 $  NODE_ENV=dev_local npm start --fp data_for_testing/csvfile.csv --mptp map_ivr_itg
 $  NODE_ENV=dev_local node start_script --fp data_for_testing/csvfile.csv --mptp map_ivr_itg

I am using nconf the command line and environment variable parser for node.js.

The problem is the command line arguments --fp and --mptp seem to disappear when using npm start.

enter image description here

Furthermore, as an aside, does any program interpret --fp as a force flag, as NPM is warning?


Source: (StackOverflow)

How do I have a namespace in file?

This JSON within a file cannot be parsed:

{
  "facebook": {
    appId: "myAppId",
    appSecret: "myAppSecret"
  }
}

When I use this:

{
  "facebook:appId": "myAppId",
  "facebook:appSecret": "myAppSecret"
}

...the following both return null...

var objFb = nconf.get("facebook");

var appId = nconf.get("facebook:appId");

How do I add a namespace within some JSON within a file?


Source: (StackOverflow)

Attaching object to Node.js process

I am using the environment variable and arguments parsing module called nconf for my node.js Express web server.

https://github.com/indexzero/nconf

I decided that the best way to make the nconf data global was to simply attach it to the process variable (as in process.env), is this a good idea or bad idea? Will it slow down execution in weighing down "process"?

Here is my code:

var nconf = require('nconf');

nconf.argv()
    .env()
    .file({ file: './config/config.json' });

nconf.defaults({
    'http': {
        'port': 3000
    }
});

process.nconf = nconf;

//now I can retrieve config settings anywhere like so process.nconf.get('key');

frankly, I kind of like this solution. Now I can retrieve the config data anywhere, without having to require a module. But there may be downsides to this...and it could quite possibly be a very bad idea. IDK.


Source: (StackOverflow)

Using nconf in the browser?

I'm looking to use https://github.com/flatiron/nconf in the browser. I have tried to use it with browserify but because nconf calls fs.readdirSync when it is required to scan directories for configuration, it fails in the browser.

// config.js
'use strict';

var nconf = require('nconf'); // this triggers the fs.readdirSync

var globalConfig = { my: { global: 1 }};
nconf.use('global', { type: 'literal', store: globalConfig });

var envConfig = { my: { env: 2 }};
nconf.use('env', { type: 'literal', store: envConfig });

module.exports = nconf;

Is it possible to use some sort of browserify transform (I didn't see a way to make it force the use of BRFS in nconf) or a way to use nconf (or some other similar library) to manage client side configuration?

If not nconf itself, then just something that would allow me to do something similar to this:

config.load('user-settings', { my : { user: 1 } });
config.load('global-settings', { my: { global: 2 } } );

config.get('my.user'); // 1
config.get('my.global'); // 2

config.unload('global-settings');

config.get('my.global'); // undefined

Source: (StackOverflow)

How can I pass a hierarchical config value to nconf on command line?

Colons separate nconf hierarchies, e.g.,

{
  "AUTH": {
    "ENABLED": true
  }
}

is accessed via:

nconf.get("AUTH:ENABLED");

I'd like to override this via environment and/or command line options under npm start, e.g.,

AUTH:ENABLED=false npm start

This fails under both bash and zsh because of the colon. Escaping the colon with \ doesn't help.

The following also fails under all circumstances (hierarchical or not):

npm start --AUTH:ENABLED=false

How can I pass a hierarchical config value through to nconf/optimist in a way that works?


Source: (StackOverflow)