How to import a Javascript file that is ESM from a CommonJS module? Gulp. Error: [ERR_REQUIRE_ESM]
How to import a Javascript file that is ESM from a CommonJS module? Gulp. Error: [ERR_REQUIRE_ESM]
我的项目完全写成 CommonJS 模块,我不打算更改它。问题是 在使用 gulp
.
时我必须使用 ESM 的库
出现这种情况的文件:
const { dest, src } = require('gulp')
const imagemin = require('gulp-imagemin') // This is a ESM. Compiler gives error [ERR_REQUIRE_ESM]
const images = () => {
// We have specific configs for jpeg and png files to try
// to really pull down asset sizes
return src('./src/images/**/*')
.pipe(
imagemin(
[
imagemin.mozjpeg({ quality: 60, progressive: true }),
imagemin.optipng({ optimizationLevel: 5, interlaced: null })
],
{
silent: true
}
)
)
.pipe(dest('./dist/images'))
}
module.exports = images
这是节点给出的错误
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\Lucas\Documents\repos\nexus-materiales\node_modules\gulp-imagemin\index.js from C:\Users\Lucas\Documents\repos\nexus-materiales\gulp-tasks\images.js not supported.
Instead change the require of index.js in C:\Users\Lucas\Documents\repos\nexus-materiales\gulp-tasks\images.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (C:\Users\Lucas\Documents\repos\nexus-materiales\gulp-tasks\images.js:2:18)
at Object.<anonymous> (C:\Users\Lucas\Documents\repos\nexus-materiales\gulpfile.js:4:16)
at async Promise.all (index 0) {
code: 'ERR_REQUIRE_ESM'
}
那么,如果它是 ESM,我如何使用 commonJS 包含这个文件?这可能吗?
要从 CommonJS 代码导入 ES 模块,请使用 dynamic import。
ES 模块导入是异步的:在创建 gulp 流之前,您必须确保 gulp-imagemin 导入已完成。这可以通过 gulp.series
.
来实现
const { dest, series, src } = require('gulp');
let imagemin;
const images = series(
async () => {
imagemin = await import('gulp-imagemin');
},
() => src('./src/images/**/*')
.pipe(
imagemin.default(
[
imagemin.mozjpeg({ quality: 60, progressive: true }),
imagemin.optipng({ optimizationLevel: 5, interlaced: null })
],
{
silent: true
}
)
)
.pipe(dest('./dist/images'))
);
请注意,我使用 imagemin.default
访问 gulp-imagemin 的默认导出,因为它是动态导入的。
我的项目完全写成 CommonJS 模块,我不打算更改它。问题是 在使用 gulp
.
出现这种情况的文件:
const { dest, src } = require('gulp')
const imagemin = require('gulp-imagemin') // This is a ESM. Compiler gives error [ERR_REQUIRE_ESM]
const images = () => {
// We have specific configs for jpeg and png files to try
// to really pull down asset sizes
return src('./src/images/**/*')
.pipe(
imagemin(
[
imagemin.mozjpeg({ quality: 60, progressive: true }),
imagemin.optipng({ optimizationLevel: 5, interlaced: null })
],
{
silent: true
}
)
)
.pipe(dest('./dist/images'))
}
module.exports = images
这是节点给出的错误
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\Lucas\Documents\repos\nexus-materiales\node_modules\gulp-imagemin\index.js from C:\Users\Lucas\Documents\repos\nexus-materiales\gulp-tasks\images.js not supported.
Instead change the require of index.js in C:\Users\Lucas\Documents\repos\nexus-materiales\gulp-tasks\images.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (C:\Users\Lucas\Documents\repos\nexus-materiales\gulp-tasks\images.js:2:18)
at Object.<anonymous> (C:\Users\Lucas\Documents\repos\nexus-materiales\gulpfile.js:4:16)
at async Promise.all (index 0) {
code: 'ERR_REQUIRE_ESM'
}
那么,如果它是 ESM,我如何使用 commonJS 包含这个文件?这可能吗?
要从 CommonJS 代码导入 ES 模块,请使用 dynamic import。
ES 模块导入是异步的:在创建 gulp 流之前,您必须确保 gulp-imagemin 导入已完成。这可以通过 gulp.series
.
const { dest, series, src } = require('gulp');
let imagemin;
const images = series(
async () => {
imagemin = await import('gulp-imagemin');
},
() => src('./src/images/**/*')
.pipe(
imagemin.default(
[
imagemin.mozjpeg({ quality: 60, progressive: true }),
imagemin.optipng({ optimizationLevel: 5, interlaced: null })
],
{
silent: true
}
)
)
.pipe(dest('./dist/images'))
);
请注意,我使用 imagemin.default
访问 gulp-imagemin 的默认导出,因为它是动态导入的。