Created
October 13, 2017 02:22
-
-
Save maumchaves/08db9f22a3223bddf463bbd17fec8dab to your computer and use it in GitHub Desktop.
Read and overwrite a json file in node.js.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const filePath = 'file/path.json'; | |
const fileObject = JSON.parse(fs.readFileSync(filePath, 'utf8')); | |
// Do something with file | |
try { | |
fs.writeFileSync(filePath, JSON.stringify(fileObject, null, 2), 'utf8'); | |
console.log("The file was saved!"); | |
} | |
catch(err) { | |
console.err("An error has ocurred when saving the file."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment