diff --git a/lib/OptiPng.js b/lib/OptiPng.js index e09dd00..71cc45c 100644 --- a/lib/OptiPng.js +++ b/lib/OptiPng.js @@ -105,13 +105,15 @@ OptiPng.prototype.end = function end(chunk) { }.bind(this)); this.readStream.once('end', function onReadStreamEnd() { this.push(null); // null closes the stream - fs.unlink(this.tempFile, function onUnlink(err){ - // not emitting this error since stream is done - if (err) { - console.error(err.stack); - } - }); - this.tempFile = null; + if (this.tempFile) { + fs.unlink(this.tempFile, function onUnlink(err){ + // not emitting this error since stream is done + if (err) { + console.error(err.stack); + } + }); + this.tempFile = null; + } }.bind(this)); }.bind(this); };