I found a code in a website for converting png, jpg to webp but it doesnt work for me. I have two jpg files in /images/ folder
const imagemin = require("imagemin"),
webp = require("imagemin-webp");
const outputFolder = "./images/webp";
const produceWebP = async () => {
await imagemin(["images/*.png"], {
destination: outputFolder,
plugins: [
webp({
lossless: true,
}),
],
});
console.log("PNGs processed");
await imagemin(["images/*.{jpg,jpeg}"], {
destination: outputFolder,
plugins: [
webp({
quality: 65,
}),
],
});
console.log("JPGs and JPEGs processed");
};
produceWebP();
When i run node index.js i take the message you see in the photo
The issue here is in
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module
.There are two types of modules in NodeJS: CommonJS and ECMAScript modules (ESM).
CommonJS uses
const webp = require("imagemin-webp")
syntax.While ESM uses
import webp from "imagemin-webp"
syntax to achieve the same result.Your
index.js
is CommonJS and the imagemin npm module is ESM and the error ocures when you try to userequire()
call to import ESM module.There are two possible solutions for this:
index.js
from CommonJS to ESM (preferred)import()
call instead ofrequire()
to import ESM module from CommonJSFirst (and preferred) option is to convert your code to ESM:
index.js
toindex.mjs
(.mjs
extension indicates ESM syntax)require()
calls toimport something from 'library'
callsnode index.mjs
index.mjs
:Second option is to use asynchronous
import()
call to import ESM modules from CommonJS module as indicated in NodeJS docs.It is not preferred because
import()
is asynchronous, I'd like to useawait
to get the result likeawait import()
but this in-turn requires to be called inside anotherasync
function.index.js
:P.S. Please note that ESM can export more than one entry (default and named exports) while CommonJS can only export one entry.