我的项目完全是使用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
这是Node报告的错误
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(ECMAScript模块),我该如何使用commonJS来包含这个文件呢?这种情况可能吗?
gulp.series
,它在这种情况下非常适用。 - Lucas David Ferrero