imagemin-mozjpeg icon indicating copy to clipboard operation
imagemin-mozjpeg copied to clipboard

Why is this error occurring (require() of ES modules is not supported.)

Open juunzzi opened this issue 2 years ago • 2 comments

I Solved It ☺️.

I think This error can be resolved by version downgrading.

or

1 ) in your package.json

Add "type":"module"

2 ) Edit code

// const imagemin = require("imagemin");
import imagemin from "imagemin"; // << edit this line 
import imageminMozjpeg from "imagemin-mozjpeg"; // << edit this line
// const imageminMozjpeg = require("imagemin-mozjpeg");
(async () => {
  const files = await imagemin(["./img/*.{jpeg,jpg,png}"], {
    destination: "./output/",
    plugins: [imageminMozjpeg({ quality: 80 })],
  });

  console.log(files);
  //=> [{data: <Buffer 89 50 4e …>, destinationPath: 'build/images/foo.jpg'}, …]
})();
then it works !!! ☺️

Below is the problem situation.

  • library ver
  • [email protected] added 42 packages from 32 contributors and audited 42 packages in 3.225s

  • [email protected] added 253 packages from 75 contributors and audited 325 packages in 9.66s

  • project folder directory

image

const imagemin = require("imagemin"); // <- error is shooting
const imageminMozjpeg = require("imagemin-mozjpeg");
(async () => {
  const files = await imagemin(["./img/*.{jpeg,jpg,png}"], {
    destination: "./output/",
    plugins: [imageminMozjpeg({ quality: 80 })],
  });

  console.log(files);
  //=> [{data: <Buffer 89 50 4e …>, destinationPath: 'build/images/foo.jpg'}, …]
})();
  • error
internal/modules/cjs/loader.js:1080
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/jangjunhyeog/Workspace/JavascriptPractice/IMAGE-OPTIMIZATION/node_modules/imagemin/index.js
require() of ES modules is not supported.
require() of /Users/jangjunhyeog/Workspace/JavascriptPractice/IMAGE-OPTIMIZATION/node_modules/imagemin/index.js from /Users/jangjunhyeog/Workspace/JavascriptPractice/IMAGE-OPTIMIZATION/script.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 /Users/jangjunhyeog/Workspace/JavascriptPractice/IMAGE-OPTIMIZATION/node_modules/imagemin/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/Users/jangjunhyeog/Workspace/JavascriptPractice/IMAGE-OPTIMIZATION/script.js:1:18)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14) {
  code: 'ERR_REQUIRE_ESM'
}

juunzzi avatar Aug 26 '21 06:08 juunzzi

I think This error can be resolved by version downgrading.

or

1 ) in your package.json

Add "type":"module"

2 ) Update code

// const imagemin = require("imagemin");
import imagemin from "imagemin";
import imageminMozjpeg from "imagemin-mozjpeg";
// const imageminMozjpeg = require("imagemin-mozjpeg");
(async () => {
  const files = await imagemin(["./img/*.{jpeg,jpg,png}"], {
    destination: "./output/",
    plugins: [imageminMozjpeg({ quality: 80 })],
  });

  console.log(files);
  //=> [{data: <Buffer 89 50 4e …>, destinationPath: 'build/images/foo.jpg'}, …]
})();

then it works !!! ☺️

juunzzi avatar Aug 26 '21 06:08 juunzzi

I using "node": "v18.14.2" and downgrading to "imagemin-mozjpeg": "^9.0.0" worked for me

phamthainb avatar Sep 24 '23 19:09 phamthainb