【问题标题】:require() of ES Module gulpfile.js not supported n gulp node jsES 模块 gulpfile.js 的 require() 不支持 n gulp node js
【发布时间】:2022-12-09 04:13:21
【问题描述】:

吞咽文件.js

'use strict';
const gulp = require("gulp");
const imagemin = require("gulp-imagemin");
const webp = require("imagemin-webp");
const extReplace = require("gulp-ext-replace");

gulp.task("exportWebP", function() {
  let src = "artists/**/*.jpg"; // Where your PNGs are coming from.
  let dest = "dist/images"; // Where your WebPs are going.

  return gulp.src(src)
    .pipe(imagemin([
      webp({
        quality: 75
      })
    ]))
    .pipe(extReplace(".webp"))
    .pipe(gulp.dest(dest));
});

包.json

{
  "type": "module",
  "main": "gulpfile.js",
  "dependencies": {
    "gulp-ext-replace": "^0.3.0",
    "gulp-imagemin": "^8.0.0",
    "imagemin-webp": "^7.0.0"
  },
  "devDependencies": {
    "gulp": "^4.0.2"
  }
}

在这里我运行 gulp js 文件它显示错误

错误 [ERR_REQUIRE_ESM]:不支持来自 gulpfile.js 的 ES 模块 index.js 的 require()。 而是将 gulpfile.js 中 index.js 的要求更改为动态 import() ,它在所有 CommonJS 模块中都可用。 在对象。 (gulpfile.js:3:18) 在异步 Promise.all(索引 0){ 代码:'ERR_REQUIRE_ESM' } 如何解决这个问题呢。

【问题讨论】:

    标签: node.js gulp nodes gulp-watch


    【解决方案1】:

    许多包移动到 esm 模块,不能再使用 require 导入

    require 更改为 import

    import gulp from "gulp";
    import imagemin from "gulp-imagemin";
    import webp from "imagemin-webp";
    import extReplace from "gulp-ext-replace";
    

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 2021-11-01
      • 1970-01-01
      • 2022-11-18
      • 2022-11-09
      • 1970-01-01
      • 2021-11-03
      • 2021-06-05
      • 2023-03-08
      相关资源
      最近更新 更多