How to use jsonfile

Comprehensive jsonfile code examples:

How to use jsonfile.readFile:

18
19
20
21
22
23
24
25
26
27
28
 * @param {string} packageName
 * @returns {Promise<string | undefined>}
 */
async function getPackageVersion(packageName) {
  let file = packageJson(packageName, "packages");
  let json = await jsonfile.readFile(file);
  return json.version;
}


/**

How to use jsonfile.readFileSync:

2
3
4
5
6
7
8
9
10
11
12
13
const jsonfile = require('jsonfile');
const axios = require('axios');
const qs = require('qs');


const configFile = './config.json';
const config = jsonfile.readFileSync(configFile);
const streamEmitter = new EventEmitter();


let startup = true;
let streams = {};

How to use jsonfile.writeFile:

167
168
169
170
171
172
173
174
175
176
177
178
}




function updateConfig(key, value) {
  config[key] = value;
  jsonfile.writeFile(configFile, config, { spaces: 2 }, function (err) {
    if (err) console.error(err)
  });
}

How to use jsonfile.writeFileSync:

45
46
47
48
49
50
51
52
53
54
55
        } else if (params[param] == 'false') {
            params[param] = false;
        }


        config[req.params.server][param] = params[param];
        jsonfile.writeFileSync('./config.json', config);
        logger.log('API', 'INFO', `Server ${req.params.server} was updated`);
    }
});
// get status of all servers