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

  1. 问题
    错误 [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.
    
  2. 原因

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

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

  4. 解决方法

    在 gulpfile.js 我改变了

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

    const imagemin = import('gulp-imagemin')

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值