Skip to content

Instantly share code, notes, and snippets.

@sgimeno
Last active August 29, 2015 13:56
Show Gist options
  • Save sgimeno/9329706 to your computer and use it in GitHub Desktop.
Save sgimeno/9329706 to your computer and use it in GitHub Desktop.
Async and sync ways to read files with FS in node.
var fs = require('fs');
var file = __dirname + '/test.json';
//Async read file
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
var data = JSON.parse(data);
console.dir(data);
});
//Sync alternative
var file = fs.readFileSync(__dirname + '/test.json', 'utf8');
var data = JSON.parse(file);
console.dir(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment