title: How to save binary buffer to png file in nodejs?
body: <p>I have binary nodejs Buffer object that contains bitmap information. How do make image from the buffer and save it to file?</p>
  
  <p>Edit:</p>
  
  <p>I tried using the file system package as @herchu said but if I do this:</p>
  
  <pre><code>let robot = require("robotjs")
  let fs = require('fs')
  
  let size = 200
  let img = robot.screen.capture(0, 0, size, size)
  
  
  let path = 'myfile.png'
  let buffer = img.image
  
  fs.open(path, 'w', function (err, fd) {
    if (err) {
      // Something wrong creating the file
    }
  
    fs.write(fd, buffer, 0, buffer.length, null, function (err) {
      // Something wrong writing contents!
    })
  })
  </code></pre>
  
  <p>I get</p>
  
  <p><a href="https://i.stack.imgur.com/AsQfB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AsQfB.png" alt="enter image description here"></a></p>