Skip to content

Instantly share code, notes, and snippets.

@jessefulton
Created January 15, 2012 17:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessefulton/1616583 to your computer and use it in GitHub Desktop.
Save jessefulton/1616583 to your computer and use it in GitHub Desktop.
Node.js - merge environment variables + config file + command line args
/**
* Merges environment variables + config file + command line args
*
* To use, from app.js:
* var conf = require('./config').init();
*/
var argv = require('optimist').argv;
function init() {
//1. check process.env
//2. config files override
//3. command line args final override
var vars = [
"GOOGLE_API_KEY",
"GOOGLE_CSE_ID",
"MONGODB_URI",
"LOGGLY_SUBDOMAIN",
"LOGGLY_INPUT_KEY"
];
var config = {};
var fileConfig = {};
try {
fileConfig = require('./conf/config.js');
} catch(e) {}
vars.forEach(function(el, idx, arr) {
config[el] = process.env[el];
if (fileConfig[el]) { config[el] = fileConfig[el]; }
if (argv[el]) { config[el] = argv[el]; }
});
return config;
}
exports.init = init;
//Fill in your accounts below and place in ./conf/config.js (see fileConfig var in config.js)
module.exports = {
GOOGLE_API_KEY: ""
, GOOGLE_CSE_ID: ""
, MONGODB_URI: ""
, LOGGLY_SUBDOMAIN: ""
, LOGGLY_INPUT_KEY: ""
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment