View on GitHub

Devendra's Log

A simple configuration mechanism in Node.js

Node.js provides a neat mechanism to read JSON, you can simply require the JSON file

var config = require('./config.json');
console.log(config.db.port);

The module system caches the file, so subsequent requires don’t parse the file again. If you need to read the file after it has been modified, you’ll need to use require.cache to delete it before invoking require.

delete require.cache('./config.json');

You can invoke require.cache when the file changes by using the watchFile function exported by the File System module.