While the following code will give you the object in procedural way you should keep in mind the asynchronous behaviour of node.
var jsonfile = require('jsonfile')
var file = 'jsonresponse.json'
var jsonObject = jsonfile.readFileSync(file)
As you are starting with node, you should get familiar with the callback pattern
In the snippet provided by you the file is being loaded in callback and you should be accessible within that scope.
var file = 'jsonresponse.json';
var jsonObject;
jsonfile.readFile(file, function(err, obj) {
//on success obj is the data therefore it holds the reference to contents of the file
if(!err){
jsonObject = obj;
}
else console.log(err);
})
jsonfile.readFile is called withe the filename and a callback function. Once it completes the task of reading file it invokes the callback function with 2 parameters: error and data.
error is null if theres no problem, otherwise it contains the error details.
data contains whatever data the function wanted to return.
fsmodule and parsing it yourself. If yourequire()the json file node will read and parse it for you. The downside is if there was an error parsing or reading it will throw an exception.