- 问题
错误 [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')
Gulp项目问题 | gulp-imagemin插件8.0版本小bug(Error [ERR_REQUIRE_ESM]: Must use import to load ES Module)
最新推荐文章于 2024-05-12 09:54:55 发布