irpas技术客

Gulp项目问题 | gulp-imagemin插件8.0版本小bug(Error [ERR_REQUIRE_ESM]: Must use import to

大大的周 4432

问题错误 [ERR_REQUIRE_ESM]:必须使用导入来加载 ES 模块:E:\gulp Project\hello-gulp\node_modules\gulp-imagemin\index.js require() 不支持 ES 模块。 E:\gulp Project\hello-gulp>gulp Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: E:\gulp Project\hello-gulp\node_modules\gulp-imagemin\index.js require() of ES modules is not supported. require() of E:\gulp Project\hello-gulp\node_modules\gulp-imagemin\index.js from E:\gulp Project\hello-gulp\gulpfile.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": " module" which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from E:\gulp Project\hello-gulp\node_modules\gulp-imagemin\package.json. 原因

node将*.js文件视为 CommonJS。而gulp-imagemin插件的package.json为 ES 模块。

相关文档package.json?和文件扩展名:Modules: Packages | Node.js v17.0.1 Documentation

解决方法

在 gulpfile.js 我改变了

const imagemin = require('gulp-imagemin');

const imagemin = import('gulp-imagemin')


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Gulp项目问题 #ERR_REQUIRE_ESM #must #use #import #To