gulp - Error [ERR_REQUIRE_ESM]: require not supported

avatar

Borislav Hadzhiev

Last updated: Feb 18, 2022

banner

Photo from Unsplash

gulp - Error [ERR_REQUIRE_ESM]: require not supported #

The gulp error "[ERR_REQUIRE_ESM]: require() not supported" occurs because the gulp-imagemin package has been converted to be an ESM only package in version 8.0.0, which means that the package cannot be imported with require() anymore.

gulp err require esm

To solve the gulp error "[ERR_REQUIRE_ESM]: require() not supported", downgrade the version of the gulp-imagemin package to 7.1.0 by running the following command: npm install --save-dev gulp-imagemin@7.1.0 . This is the last version of gulp-imagemin that is built with CommonJS.

To install version 7.1.0 of the gulp-imagemin package, open your terminal in the root directory of your project and run the following command.

shell
npm install --save-dev gulp-imagemin@7.1.0 # 👇️ Only run this if using TypeScript npm install --save-dev @types/gulp-imagemin@7.0.3

Now you are able to use the gulp and gulp-imagemin packages with the require() syntax.

index.js
const gulp = require('gulp'); const imagemin = require('gulp-imagemin'); console.log(gulp); console.log(imagemin);

If your environment supports ES Modules, you should try to convert the require() import to ESM.

index.js
import gulp from 'gulp'; import imagemin from 'gulp-imagemin'; console.log(gulp); console.log(imagemin);

But this might not work, depending on your setup.

An ESM package basically means that you should be using import imagemin from 'gulp-imagemin' instead of const imagemin = require('gulp-imagemin') (if your environment supports it).

If you want to read more about ESM packages and why more and more packages are adopting ES modules, check out this Github Readme file.

Conclusion #

To solve the gulp error "[ERR_REQUIRE_ESM]: require() not supported", downgrade the version of the gulp-imagemin package to 7.1.0 by running the following command: npm install --save-dev gulp-imagemin@7.1.0 . This is the last version of gulp-imagemin that is built with CommonJS.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.