Javascript 尝试使用imagemin webp将图像转换为webp,但不起作用
我在一个网站上找到了一个将png、jpg转换为webp的代码,但它对我不起作用。我在/images/文件夹中有两个jpg文件Javascript 尝试使用imagemin webp将图像转换为webp,但不起作用,javascript,imagemin-webp,Javascript,Imagemin Webp,我在一个网站上找到了一个将png、jpg转换为webp的代码,但它对我不起作用。我在/images/文件夹中有两个jpg文件 const imagemin = require("imagemin"), webp = require("imagemin-webp"); const outputFolder = "./images/webp"; const produceWebP = async () => { await i
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();
当我运行node index.js时,我会记录您在照片中看到的消息
这里的问题在于
错误[ERR\u REQUIRE\u ESM]:必须使用导入来加载ES模块
NodeJS中有两种类型的模块:和
CommonJS使用const webp=require(“imagemin webp”)
语法
而ESM使用从“imagemin webp”导入webp
语法来实现相同的结果
您的index.js
是CommonJS,是ESM,当您尝试使用require()
调用导入ESM模块时,会出现错误
有两种可能的解决方案:
- 将您的
从CommonJS转换为ESM(首选)index.js
- 使用异步
调用而不是import()
从CommonJS导入ESM模块require()
- 将
重命名为index.js
(index.mjs
扩展名表示ESM语法).mjs
- 将所有
调用更改为require()
callsimport something from'library'
- 将其作为
节点索引.mjs运行
index.mjs
:
// using ES import syntax here
import imagemin from "imagemin";
import webp from "imagemin-webp";
// the rest of the file is unchanged
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();
const outputFolder = "./images/webp";
const produceWebP = async () => {
// Load ESM modules using import(),
// it returns a Promise which resolves to
// default export as 'default' and other named exports.
// In this case we need default export.
const imagemin = (await import("imagemin")).default;
const webp = (await import("imagemin-webp")).default;
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();
第二个选项是使用异步import()
调用从CommonJS模块导入ESM模块,如NodeJS中所示
它不是首选的,因为import()
是异步的,我想使用await
获得类似await import()
的结果,但这反过来需要在另一个async
函数中调用
index.js
:
// using ES import syntax here
import imagemin from "imagemin";
import webp from "imagemin-webp";
// the rest of the file is unchanged
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();
const outputFolder = "./images/webp";
const produceWebP = async () => {
// Load ESM modules using import(),
// it returns a Promise which resolves to
// default export as 'default' and other named exports.
// In this case we need default export.
const imagemin = (await import("imagemin")).default;
const webp = (await import("imagemin-webp")).default;
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();
附言。
请注意,ESM可以导出多个条目(默认和命名导出),而CommonJS只能导出一个条目。这是最好的解释!非常感谢你!