imagemin([__dirname + 'images/raw/*.{jpg,png}'], {
destination: __dirname + '/images/converted/',
plugins: [
imageminWebp({
quality: 75,
resize: {
width: 1000,
height: 0
}
})
]
})
.then(() => {
console.log('Images optimized');
})
The code is working fine and I get the message "Images optimized" but the destination folder is empty. Nothing is happening with the code. Can someone help me out with this?
The problem is with your input path. __dirname
strips the trailing slash, so assuming your working directory is /Users/user/my-project
, you end up with a glob like this:
/Users/user/my-projectimages/raw/*.{jpg,png}
.
It is generally advised to join paths via path.join()
for consistency:
const path = require('path');
const inputPath = path.join(__dirname, 'images/raw/*.{jpg,png}');
// /Users/user/my-project/images/raw/*.{jpg,png}