How can I specify different config files in Node.js?

Problem

try
config = JSON.parse(fs.readFileSync('./config.json', 'ascii'))
catch e
util.debug("Could not parse configuration file.\n" + e.stack)
return

I have that in my node.js code and I have two config files"

  • config.json.prod
  • config.json.local

How can I specify that it should run the .local?

By the way, I’m using CoffeeScript

Problem courtesy of: Shamoon

Solution

Yea, you certainly can. Checkout nodepad (great reference app):

The source comes from this blog series: http://dailyjs.com/2010/11/01/node-tutorial/

Edit: For example

if (app.settings.env == 'production') {
mailer.send(mailOptions,
function(err, result) {
if (err) {
console.log(err);
}
}
);
}
he uses app.settings.env to swap out different environmental/app settings. Same could be used to load a config file. — Chance

Solution courtesy of: Chance

View additional discussion.