title: (node.js module) sharp image processor keeps source file open, unable to unlink original after resize
body: <p>I'm using <a href="http://sharp.dimens.io/en/stable/" rel="noreferrer">sharp</a> to resize an uploaded image in a <code>Node.js</code> / <code>Express</code> application written in <code>Typescript</code>. After successful resizing, I'd like to delete the original file. For <code>png</code> and <code>gif</code> input images, the operation terminates successfully, I have the resized image and the original is deleted. For <code>jpg</code> and <code>tif</code> images, the resize is successful, however the <code>unlink</code> command fails with the following error:</p>
  
  <blockquote>
    <p>EBUSY: resource busy or locked, unlink '...'</p>
  </blockquote>
  
  <p>as if the <code>sharp().resize()</code> would still keep the input file locked, even after the completion of the resize operation.</p>
  
  <p>Here is the code to test the described behavior:</p>
  
  <pre><code>import { existsSync, unlinkSync } from "fs";
  import { normalize, parse } from "path";
  
  var argv = require("yargs").argv;
  var sharp = require("sharp");
  var appRoot = require("app-root-path") + "/";
  
  let resizeTest = async function (filename: string): Promise&lt;boolean&gt; {
      try {
          let nameParts = parse(filename);
          let source = appRoot + filename;
          let destination = appRoot + nameParts.name + "_resized" + nameParts.ext;
          let fileExists = await existsSync(source);
          if (!fileExists) {
              console.log("Input file not found. Exiting.");
              return false;
          }
  
          let resizeResult = await sharp(source)
              .resize(128, 128)
              .toFile(destination);
          console.log("Resize operation terminated: ", resizeResult);
  
          await unlinkSync(source);
          console.log("unlinkSync operation terminated.");
  
          return true;
      } catch (error) {
          console.log("An error occured during resizeTest execution: ", error.message);
          return false;
      }
  }
  
  if (argv._.length === 0) {
      console.log("Usage: node sharptest.js FILENAME");
  } else {
      let resizeResult: Promise&lt;boolean&gt; = resizeTest(argv._[0]);
      resizeResult.then(result =&gt; console.log("Returning from execution with ", result));
  }
  </code></pre>
  
  <p>What am I missing?</p>