Search code examples
node.jsimagebuffersharp

Node.js: Sharp package unable to install


I'm trying to install the Sharp package to compress buffers for images. Now when I install the package via npm install sharp --save, and try to use this line of code:

const sharp = require('sharp');

I get the following error: https://textuploader.com/1gvep . This is the mentioned log: https://textuploader.com/1gvez . I did some research before posting this. This is what I've tried:

  • use --ignore-scripts=false flag
  • use --unsafe-perm flag
  • remove sharp package from node_modules and run npm install --ignore-scripts=false again
  • run npm rebuild

But nothing of this would help so far. Now some people seemed to solve the problem by downgrading to Node 10, but this is not an option for me because I need to use it together with Discord.js which requires at least Node 12. The idea I'd have in mind is writing the image to a tmp folder, compressing the written image with another libarary and reload it, but that's not very elegant in my opinion. Now my questions are:

  • What else can I try to install Sharp correctly?
  • Are there alternatives to shrink image buffer sizes down?

Edit: As suggested by Alex, I tried out Jimp and simply reduced the image size for the beginning. I'll share my code here in case somebody looks for an alternative.

let buffer = canvas.toBuffer('image/png'); //from node-canvas
Jimp.read(buffer)
.then(image => {
   image.resize(width>>1, height>>1).getBufferAsync(Jimp.MIME_PNG)
   .then(comprBuffer => {
      /* use the new buffer */
   });
})
.catch(err => { 
   console.log(err) 
});

Solution

  • I tried installing sharp and had no issues on the following versions:

    Node v12.16.1 Npm v6.13.4

    How about using Jimp instead?

    https://www.npmjs.com/package/jimp