Search code examples
node.jstypescriptimagemin

imagemin - code executes but nothing seems to happens


I'm trying to compress an image in Node 14 with Typescript and using imagemin.

I took the examples I found online and run the code, but nothing seems to happen, it finishes without any errors. The output is empty as well.

imagemin version 7.0.1
node v14.15.4

'use strict';
import imagemin from 'imagemin';
import imageminJpegtran from 'imagemin-jpegtran';

export async function compressImage(): Promise<void> {

const files = await imagemin(['../David.jpeg'], {
    destination: '../compressed-images',
    plugins: [
        imageminJpegtran(),
    ]
});

console.log(JSON.stringify(files, null, 1));
}

I build the code and then run it but the output is simply an empty array []. Also, nothing appears in the ../compressed-images folder.

Please advise on how I can resolve this.


Solution

  • Issue was due to the path not being passed as an absolute path. This is the working code:

    const pv = path.join(__dirname, '../../David.jpeg');
    
    const files = await imagemin([pv], {
        destination: path.join(__dirname, '../../compressed-images'),
        plugins: [
            imageminMozjpeg({quality: 50})
        ]
    });