简体   繁体   中英

require() of ES Module gulpfile.js not supported n gulp node js

gulpfile.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));
});

package.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"
  }
}

here i run gulp js file it show the error

Error [ERR_REQUIRE_ESM]: require() of ES Module index.js from gulpfile.js not supported. Instead change the require of index.js in gulpfile.js to a dynamic import() which is available in all CommonJS modules. at Object. (gulpfile.js:3:18) at async Promise.all (index 0) { code: 'ERR_REQUIRE_ESM' } how to solve this problem.

Many packages moved to esm modules and can no longer be imported using require

Change require to import

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM