how to update a json file javascript

Solutions on MaxInterview for how to update a json file javascript by the best coders in the world

showing results for - "how to update a json file javascript"
Gaia
27 Mar 2020
1const fs = require('fs');
2const fileName = './file.json';
3const file = require(fileName);
4    
5file.key = "new value";
6    
7fs.writeFile(fileName, JSON.stringify(file), function writeJSON(err) {
8  if (err) return console.log(err);
9  console.log(JSON.stringify(file));
10  console.log('writing to ' + fileName);
11});
12