Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created September 13, 2015 13:06
Show Gist options
  • Save bennadel/b1d15ab50a4fe80ba9bc to your computer and use it in GitHub Desktop.
Save bennadel/b1d15ab50a4fe80ba9bc to your computer and use it in GitHub Desktop.
You Can Use require() To Load JSON (JavaScript Object Notation) Files In Node.js
module.exports = {
server: "localhost",
port: 1234,
timeout: 10
};
{
"server": "localhost",
"port": 1234,
"timeout": 10
}
// When loading a configuration file, we have the choice to load either .js file,
// which will be interpreted as a JavaScript module file, with the "exports" being
// returned from the require; or, we can load a .json file, which will be parsed as
// JavaScript Object Notation (JSON) with the result being returned from the require.
// Load configuration as JSON.
console.log( "JSON File:" );
console.log( require( "./config.json" ) );
// Load configuration as module.
console.log( "MODULE File:" );
console.log( require( "./config.js" ) );
// When loading a configuration file, we don't have to lock ourselves into a particular
// type of file. If we exclude the file-extension, Node will automatically try to look
// for a *.js and then, if not found, a *.json file. This means that we can start out
// using a .json file; then, if we need to add a programmatic aspect to the config file,
// we can transparently change it over to a "module" style file. The take-away here is
// that there is absolutely no reason to NOT start out with a .json file if your module
// just returns a static hash.
// Load configuration as UNKNOWN file type.
// --
// NOTE: On disk, it is "config.json". But, we're going to make Node.js look for it.
console.log( "UNKNOWN File:" );
console.log( require( "./config" ) );
@aimtiaz11
Copy link

One thing to note is that if the JSON file is not semantically correct, node wont pick it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment