Created
November 15, 2012 02:20
-
-
Save cbumgard/4076234 to your computer and use it in GitHub Desktop.
Dynamic config module for node.js apps to simplify managing configurations for different environments. Reads in a config file matching the current NODE_ENV.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This module loads a config file in the current working directory matching the NODE_ENV variable. | |
// I.e. either './development.js' or './production.js' based on the process.env.NODE_ENV variable. | |
// If not set, it defaults to './development.js'. | |
// Can load custom environment files as well, as long as the NODE_ENV variable matches | |
// a file in the current directory. E.g. './staging.js' | |
// Usage: calling code can just require this module, e.g. "var config = require('./config')" | |
// assuming this file is named "index.js" and lives in a subdirectory named "config" of the app root. | |
var config | |
, config_file = './' + (process.env.NODE_ENV ? process.env.NODE_ENV : 'development') + '.js'; | |
try { | |
config = require(config_file); | |
} catch (err) { | |
if (err.code && err.code === 'MODULE_NOT_FOUND') { | |
console.error('No config file matching NODE_ENV=' + process.env.NODE_ENV | |
+ '. Requires "' + __dirname + '/' + process.env.NODE_ENV + '.js"'); | |
process.exit(1); | |
} else { | |
throw err; | |
} | |
} | |
module.exports = config; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice gist. I wrote a module which does almost what you do, but it accepts an "env" to be passed via args optionally.
Maybe it helps :)
https://github.com/peerigon/dynamic-config
I will add the check for
NODE_ENV
soon.