Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js global configuration

I'm currently working on a small console project that depends a lot on the arguments that are passed initially and I'm looking for a good way to handle a configuration object in nodejs.

I have the project currently fully working with the following example but I think I'm relaying on the caching of modules when using 'require'.

lets assume a module options.js

'use strict';

var options = {
    configName: '.jstail',
    colorActive: (process.platform === 'win32') ? false : true, // deactivate color by default on windows platform
    quiet: false,
    debug: false,
    config: null,
    logFile: null,
    setting: null
};

module.exports = options;

And my initial module init.js

#!/usr/bin/env node
'use strict';

var options = require('options'); // require above options module

// modify the options object based on args

I then have a logger that depends on this options

For example if quiet is set to true no logging should happen

logger.js

'use strict';

var options = require('options');

/**
 * prints to console if not explicitly suppresed
 * @param {String} text
 */
function log(text) {
   if (!options.quiet) {
       console.log('[LOG]: ' + text);
   }
}

My big problem is (I think) that I'm relaying on the caching of nodejs modules when I require the options module in the logger

So my two questions are:

Am I right that this only works because of the caching of the modules that nodejs does for me? Is there any better way to handle a dynamic global configuration?

I know there are several questions and tutorials around with a config file but thats not what I'm looking for.

like image 837
Playerwtf Avatar asked Nov 19 '13 19:11

Playerwtf


People also ask

What is global nodeJS?

Node. js global objects are global in nature and they are available in all modules. We do not need to include these objects in our application, rather we can use them directly. These objects are modules, functions, strings and object itself as explained below.

What is configuration in nodeJS?

Node-config allows you to create configuration files in your Node application for different deployment environments. With it, you can define a default configuration file that you intend to repeat across environments, then extend the default config to other environments, such as development, staging, etc.


1 Answers

Yes, this only works because of caching, though I wouldn't call it caching (but node.js docs do) rather than lazy initialization. It's ok to rely on that, a lot of modules do some initialization of first require, using it for configuration is also typical. Generally speaking, require is a node.js way of accessing global singleton objects.

The other way to do it is to load configuration from a single file, modify it and then pass it to other modules who need it, like this:

//index.js
var config = require('./config')

config.flag = false

var module1 = require('./module1')(config)

//module1.js
module.exports = function (config) {
// do stuff
}

It makes code more decoupled and testable but adds complexity. Difference between these two approaches is basically the same as using globals vs dependency injection. Use whatever you like.

like image 140
vkurchatkin Avatar answered Sep 28 '22 20:09

vkurchatkin