Y
Y
Yeldos Sabyrbayev2021-08-18 11:59:00
gulp.js
Yeldos Sabyrbayev, 2021-08-18 11:59:00

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module?

History
I did the gulp setup according to the video lesson. At first everything worked fine, but as I got to the installation of the "imagemin" plugin, problems arose. Installed the plugin using the code npm install --save-dev gulp-imagemin

Code

let project_folder = "dist";
let source_folder = "#src";

let path = {
  build: {
    html: project_folder + "/",
    css: project_folder + "/css/",
    js: project_folder + "/js/",
    img: project_folder + "/img/",
    fonts: project_folder + "/fonts/",
  },
  src: {
    html: [source_folder + "/*.html", "!" + source_folder + "/_*.html"],
    css: source_folder + "/scss/style.scss",
    js: source_folder + "/js/script.js",
    img: source_folder + "/img/**/*.{jpg,png,svg,gif,ico,webp}",
    fonts: source_folder + "/fonts/*.ttf",
  },
  watch: {
    html: source_folder + "/**/*.html",
    css: source_folder + "/scss/**/*.scss",
    js: source_folder + "/js/**/*.js",
    img: source_folder + "/img/**/*.{jpg,png,svg,gif,ico,webp}",
  },
  clean: "./" + project_folder + "/"
}

let { src, dest } = require('gulp'),
  gulp = require('gulp'),
  browsersync = require("browser-sync").create(),
  fileinclude = require("gulp-file-include"),
  del = require("del"),
  scss = require('gulp-sass')(require('sass')),
  autoprefixer = require("gulp-autoprefixer"),
  group_media = require("gulp-group-css-media-queries"),
  clean_css = require("gulp-clean-css"),
  rename = require("gulp-rename"),
  uglify = require("gulp-uglify-es").default,
  imagemin = require("gulp-imagemin"),
  webp = require('gulp-webp');

function browserSync(params) {
  browsersync.init({
    server: {
      baseDir: "./" + project_folder + "/"
    },
    port: 3000,
    notify: false
  })
}

function html() {
  return src(path.src.html)
    .pipe(fileinclude())
    .pipe(dest(path.build.html))
    .pipe(browsersync.stream())
}

function css() {
  return src(path.src.css)
    .pipe(
      scss({
        outputStyle: "expanded"
      }).on('error', scss.logError)
    )
    .pipe(
      group_media()
    )
    .pipe(
      autoprefixer({
        overrideBrowserslist: ["last 5 versions"],
        cascade: true
      })
    )
    .pipe(dest(path.build.css))
    .pipe(clean_css())
    .pipe(
      rename({
        extname: ".min.css"
      })
    )
    .pipe(dest(path.build.css))
    .pipe(browsersync.stream())
}

function js() {
  return src(path.src.js)
    .pipe(fileinclude())
    .pipe(dest(path.build.js))
    .pipe(
      uglify()
    )
    .pipe(
      rename({
        extname: ".min.js"
      })
    )
    .pipe(dest(path.build.js))
    .pipe(browsersync.stream())
}

function images() {
  return src(path.src.img)
    .pipe(dest(path.build.img))
    .pipe(
      webp({
        quality: 70
      }))
    .pipe(src(path.src.img))
    .pipe(
      imagemin({
        progressive: true,
        svgoPlugins: [{ removeViewBox: false }],
        interlaced: true,
        optimizationLevel: 3
      })
    )
    .pipe(dest(path.build.img))
    .pipe(browserSync.stream())
}

function watchFiles(params) {
  gulp.watch([path.watch.html], html);
  gulp.watch([path.watch.css], css);
  gulp.watch([path.watch.js], js);
  gulp.watch([path.watch.img], images);
}

function clean(params) {
  return del(path.clean);
}

let build = gulp.series(clean, gulp.parallel(js, css, html, images));
let watch = gulp.parallel(build, watchFiles, browserSync);

exports.images = images;
exports.css = js;
exports.css = css;
exports.html = html;
exports.build = build;
exports.watch = watch;
exports.default = watch;


Compiler
611ccb5d6a144968485985.jpeg

I understand there are some problems with the module. That is, it should be "ES module, and I use "CommonJS" (whatever that means). I read on the forums to create another "package.json" in the "src" folder, and write it there {"type": "module"}. But it did not help.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Ukolov, 2021-08-18
@yeldos23

Well, after all, it is written in English-in-white: must use import :
import imagemin from "gulp-imagemin"

A
aidyn_99, 2021-10-03
@aidyn_99

I also had this error, I solved it by reinstalling npm i [email protected]

R
Razunter, 2021-10-04
@Razunter

Yes, gulp-imagemin v8 switched to ESM, all configs for it must be redone or rolled back to the previous version.
I redid:

  • did in package.json"type": "module"
  • rewrote gulpfile.js to esm with constructs like
    import gulp from 'gulp'
    const {series, parallel, watch, src, dest, task} = gulp

  • postcss config just renamed to postcss.config.cjs

D
Djoni Kage, 2021-11-30
@djonikage

So, did you find the answer to your question?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question