You mix asynchronous and synchronous wrong. You are confusing different things.
Your code should be the same (synchronously):
try { var json = JSON.parse(fs.readFileSync('updates.json')); } catch (err) { console.error(err); }
or asynchronously:
fs.readFile('updates.json', function(err,data) { if (err) { console.error(err); } else { var json = JSON.parse(data); } });
The difference is that fs.readFile ( docs ) is expecting a callback and will give you an error / result by calling the specified callback. It does not return anything.
And fs.readFileSync ( docs ) does not accept the callback because it is synchronous and returns a result or throws an error.
Also, if you are parsing .json statically, you can use require :
var json = require('./updates')
Note that the require function will cache its output, and in subsequent runs it will return the same object without blocking or performing any I / O.
source share