Checking if writeFileSync successfully wrote the file

According to node.js source-code fs.writeFileSync doesn't return anything.

It throws an Error object if something goes wrong. So you should write fs.writeFileSync(file, content, 'utf8'); within a try-catch block.


fs.writeFileSync does not return any value, if there is no exception happens that means the save succeeded; otherwise failed.

you may want to try the async version of file read

fs.exists(file, function (exists) {
  if (exists) {
    fs.writeFiles(file, content, 'utf-8', function (err) {
      if (err) {
        response.send("failed to save");
      } else {
        response.send("succeeded in saving");
      }
  } else {
    console.log('file does not exists');
  }
}