Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
juunzzi opened this issue Aug 26, 2021 · 2 comments
Open

Comments

@juunzzi
Copy link

juunzzi commented Aug 26, 2021

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
Copy link
Author

juunzzi commented Aug 26, 2021

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 !!! ☺️

@phamthainb
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants