Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50655 views
1
/*
2
* file.js: Simple utilities for working with the file system.
3
*
4
* (C) 2011, Nodejitsu Inc.
5
* MIT LICENSE
6
*
7
*/
8
9
var fs = require('fs');
10
11
exports.readJson = exports.readJSON = function (file, callback) {
12
if (typeof callback !== 'function') {
13
throw new Error('utile.file.readJson needs a callback');
14
}
15
16
fs.readFile(file, 'utf-8', function (err, data) {
17
if (err) {
18
return callback(err);
19
}
20
21
try {
22
var json = JSON.parse(data);
23
callback(null, json);
24
}
25
catch (err) {
26
return callback(err);
27
}
28
});
29
};
30
31
exports.readJsonSync = exports.readJSONSync = function (file) {
32
return JSON.parse(fs.readFileSync(file, 'utf-8'));
33
};
34
35