当前位置:   article > 正文

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

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 模块。
    1. E:\gulp Project\hello-gulp>gulp
    2. Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: E:\gulp Project\hello-gulp\node_modules\gulp-imagemin\index.js
    3. require() of ES modules is not supported.
    4. 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": "
    5. module" which defines all .js files in that package scope as ES modules.
    6. 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')

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/550905
推荐阅读
相关标签
  

闽ICP备14008679号