title: Node.js fs.writeFileSync with options and callback and error hundler?
body: <p>I looked at <a href="https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options" rel="nofollow noreferrer">Node.js Documentation</a> and I didn't find how to apply options (settings) and a callback function with error handling. And I have to use .writeFileSync (not asynchronous .writeFile):</p>
  
  <pre><code>const settings = {
          flags: 'w',
          encoding: null, //must be null
          mode: 0o666,
          autoClose: true //not found this option
        };
  
  fs.writeFileSync(dest, buff, settings);
  </code></pre>
  
  <p>Before I used:  </p>
  
  <pre><code>fs.writeFileSync(dest, buff, function (err) {
    if (err) { 
      ... 
    } else { ... console.log("OK") }
  
  })
  </code></pre>
  
  <p>but I found that I have to apply <strong>encoding: null,</strong> option to prevent any modifications of source data (buff), in other case the file can be broken. </p>
  
  <p>Edit:
  After amazing answers and explanations I would like to say that I was confused with Node.js Documentation :
  <strong>fs.writeFileSync(file, data[, options])</strong> is </p>
  
  <blockquote>
    <p><strong>The synchronous version of fs.writeFile().</strong></p>
  </blockquote>
  
  <p>Since this is version of fs,writeFile() method I thought it can have the same versions of function's signature...</p>
  
  <p>And here my final version of code, but <strong>it still has the issue with decoding  of binary files (can be any file types)</strong> (* by the way when I tried to use Axios.js I saw Errors: "Request failed with Status Code 500):
  <a href="https://i.stack.imgur.com/vnIoz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vnIoz.png" alt="enter image description here"></a></p>
  
  <pre><code>function download(url, dest, fileName, callback) {
  
    //import http from 'http';
    var request = http.get(url, function (response) {
  
      var bodyParts = [];
      var bytes = 0;
  
      response.on("data", function (c) {
        bodyParts.push(c);
        bytes += c.length;
      })
  
      response.on("end", function () {
  
        // flatten into one big buffer
        var buff = new Buffer(bytes);
        var copied = 0;
  
        for (var i = 0; i &lt; bodyParts.length; i++) {
          bodyParts[i].copy(buff, copied, 0);
          copied += bodyParts[i].length;
        }
  
        const settings = {
          flags: 'w',
          encoding: null, //not applicable / no changes
          mode: 0o666
        };
  
        try {
          fs.writeFileSync(dest, buff, settings);
  
          let msgOK = {
            filename: fileName,
            status: 'OK',
            text: `File downloaded successfully`
          }
          if (callback) callback(msgOK);
          console.log(msgOK.text);
          isLoading = false; //IMPORTANT! 
  
        } catch (err) {
          console.error(err.stack || err.message);
  
          let msgErr = {
            filename: fileName,
            status: 'ERROR',
            text: `Error in file downloading ${err.message}`
          }
          ERRORS.push(err);
          if (callback) callback(msgErr);
        }
  
      })
  
    })
  
  }
  </code></pre>