Search code examples
node.jswebpimageminimagemin-webp

Convert Images to webp with Node


I have a script to convert images to node, but am having an issue where I receive the successful message, yet nothing is output.

import imagemin from "imagemin";
import webp from "imagemin-webp";

var outputFolder = "./FSS-assets/webp";            // Output folder
var PNGImages = "./FSS-assets/*.png";         // PNG images
var JPEGImages = "./FSS-assets/*.jpg";        // JPEG images

imagemin([PNGImages], outputFolder, {
  plugins: [webp({
      lossless: true // Losslessly encode images
  })]
}).then(function() {
  console.log("Images converted!");
});

imagemin([JPEGImages], outputFolder, {
  plugins: [webp({
    quality: 65 // Quality setting from 0 to 100
  })]
}).then(function() {
  console.log("Images converted!");
});

I am running

  • node v14.16.0
  • imagemin v8
  • imagemin-webp v6

I have also tried using a relative folder path to no avail. This is on Windows 10.


Solution

  • That's because imagemin has 2 params input and options You are giving 3params.

    From the Doc:

    imagemin(input, options?)
    
    Returns Promise<object[]> in the format {data: Buffer, sourcePath: string, destinationPath: string}.
    

    So for your code it will be:

    imagemin([PNGImages], {
      destination: outputFolder,
      plugins: [webp({
          lossless: true // Losslessly encode images
      })]
    }).then(function() {
      console.log("Images converted!");
    });