Created
September 13, 2015 13:06
-
-
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
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
module.exports = { | |
server: "localhost", | |
port: 1234, | |
timeout: 10 | |
}; |
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
{ | |
"server": "localhost", | |
"port": 1234, | |
"timeout": 10 | |
} |
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
// 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" ) ); |
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
// 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" ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One thing to note is that if the JSON file is not semantically correct, node wont pick it up.