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

require() of ES modules is not supported. #380

Closed
krishnaTORQUE opened this issue Apr 23, 2021 · 25 comments
Closed

require() of ES modules is not supported. #380

krishnaTORQUE opened this issue Apr 23, 2021 · 25 comments

Comments

@krishnaTORQUE
Copy link

After update to latest version I got this error
require() of ES modules is not supported.
I am using typescript

@krishnaTORQUE
Copy link
Author

I tried

const imagemin = require('imagemin');

import imagemin from 'imagemin';

import * as imagemin from 'imagemin';

import { imagemin } from 'imagemin';

None of those above worked

@krishnaTORQUE
Copy link
Author

error log

require() of ES modules is not supported.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from node_modules/imagemin/package.json.

@houzigou
Copy link

Go back to the 7.0.1 version

@krishnaTORQUE
Copy link
Author

Yes this is what I did for now. So there will be no support for typescript in future ?

@daorren
Copy link

daorren commented May 13, 2021

same for javascript here
Node version: 14.16.0

@jrmyio
Copy link

jrmyio commented May 13, 2021

Same issue, tested multiple node versions , none is working.

Downgrading to ^7.0.1 again works.

@fiznool
Copy link

fiznool commented May 17, 2021

@MihaelN
Copy link

MihaelN commented Jun 21, 2021

This is still an issue, even reverting back to ^7.0.1 still causes this issue.

import imagemin from 'imagemin'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47
npm -v
6.14.13
 node -v
v14.17.1

@benjaminboruff
Copy link

benjaminboruff commented Jun 21, 2021

Going back to ^7.0.1 using const imagemin = require('imagemin'); worked for me.

That kinda sucks. There aren't many good alternatives out there for programmatic image processing. This repo is now unmaintained, and the (former) author recommends squoosh , but that is ESM, too. :(

Don't get me wrong, I think ESM is the way to go, but for many projects it will require (ha, ha) a lot of work. I use eleventy and while ESM is listed as an issue, it won't be dealt with anytime soon. For now, it seems like it will be a safer bet to just use CLIs to process images, then copy them to where they need to go before deploying. Sheesh.

@krishnaTORQUE
Copy link
Author

Its still not working this way in typescript

// @ts-ignore
const imagemin: any = require('imagemin');

Error

Must use import to load ES Module
require() of ES modules is not supported.

@MihaelN
Copy link

MihaelN commented Jun 22, 2021

Going back to ^7.0.1 using const imagemin = require('imagemin'); worked for me.

That kinda sucks. There aren't many good alternatives out there for programmatic image processing. This repo is now unmaintained, and the (former) author recommends squoosh , but that is ESM, too. :(

Don't get me wrong, I think ESM is the way to go, but for many projects it will require (ha, ha) a lot of work. I use eleventy and while ESM is listed as an issue, it won't be dealt with anytime soon. For now, it seems like it will be a safer bet to just use CLIs to process images, then copy them to where they need to go before deploying. Sheesh.

I am working on migrating over to @squosh/cli, for the time being I will have to revert back to require + v7.0.1, thanks 👍

@benjaminboruff
Copy link

@krishnaTORQUE I was able to get it to compile in ts like so:

package.json


"devDependencies": {
    "typescript": "^4.3.4",
    "imagemin": "^7.0.1"
  }

src/main.ts

// @ts-ignore
const imagemin: any = require('imagemin');

then compile npx ts src/main.ts, and the compiled dist/main.js is

"use strict";
// @ts-ignore
var imagemin = require('imagemin');

So I'm not sure why it doesn't work for you!
Sorry!

@krishnaTORQUE
Copy link
Author

@benjaminboruff thank you for your reply. I am very much unsure about it.
But I tried with v8 if you are talking about v7 then yes its working like this way.

@benjaminboruff
Copy link

@krishnaTORQUE sorry, I should have specified ... I am using node 16.3.0 so this is just at the command-line (linux ubuntu) and I use it in a static site generator on my laptop.

But, yeah, sadly I think we all need to find a different package :(
Good luck!

@TomoyaKuroda
Copy link

I don't know if you guys figured out the solution, but I just changed the file extension from js to mjs and replace the require with import, and then everything works fine with version 8.
So something like this.

webp.mjs

import imagemin from 'imagemin';
import imageminWebp from "imagemin-webp";

imagemin(["./images/*.{jpg,png}"], {
	destination: "./images/webp/",
	plugins: [
		imageminWebp({
			//   quality: 90
			//   ,
			//   resize: {
			//     width: 1000,
			//     height: 0
			//   }
		}),
	],
}).then(() => {
	console.log("Images Converted Successfully!!!");
});

and run node .\webp.mjs

@krishnaTORQUE
Copy link
Author

I don't know if you guys figured out the solution, but I just changed the file extension from js to mjs and replace the require with import, and then everything works fine with version 8.
So something like this.

webp.mjs

import imagemin from 'imagemin';
import imageminWebp from "imagemin-webp";

imagemin(["./images/*.{jpg,png}"], {
	destination: "./images/webp/",
	plugins: [
		imageminWebp({
			//   quality: 90
			//   ,
			//   resize: {
			//     width: 1000,
			//     height: 0
			//   }
		}),
	],
}).then(() => {
	console.log("Images Converted Successfully!!!");
});

and run node .\webp.mjs

Your solution won't work in Typescript

@krishnaTORQUE
Copy link
Author

This issue has not been solved in v8.0.1

@duan602728596
Copy link

Because the imagemin 8 version is now pure ESM.
If you want to use it in the commonjs module system, you need to load it in the following way:

const imageminJpegtran = require('imagemin-jpegtran');

async function main() {
  const imagemin = (await import('imagemin')).default;
  const files = await imagemin(['images/*.{jpg,png}'], {
    destination: 'build/images',
    plugins: [imageminJpegtran()]
  });
}

main();

About pure ESM package

@krishnaTORQUE
Copy link
Author

krishnaTORQUE commented Aug 13, 2021

Because the imagemin 8 version is now pure ESM.
If you want to use it in the commonjs module system, you need to load it in the following way:

const imageminJpegtran = require('imagemin-jpegtran');

async function main() {
  const imagemin = (await import('imagemin')).default;
  const files = await imagemin(['images/*.{jpg,png}'], {
    destination: 'build/images',
    plugins: [imageminJpegtran()]
  });
}

main();

About pure ESM package

Awesome. That resolved the issue in typescript too

takuti added a commit to takuti/takuti.me that referenced this issue Aug 29, 2021
@KulaGGin
Copy link

Go back to the 7.0.1 version

Thanks. It's 7.1.0, though. There is no 7.0.1 version(in npm at least):
phpstorm64_gnmevugPt1

@Jax-p
Copy link

Jax-p commented Dec 10, 2021

Awesome. That resolved the issue in typescript too

Not for me. It just postponed error until the dynamic import. Last working version for me is still 7.X.


Thanks. It's 7.1.0, though. There is no 7.0.1 version(in npm at least):

No it is not 7.1.0. It is 7.0.1. Last 4 versions on npm are: 8.0.1, 8.0.0, 7.0.1, 7.0.0

@KulaGGin
Copy link

KulaGGin commented Dec 10, 2021

No it is not 7.1.0. It is 7.0.1. Last 4 versions on npm are: 8.0.1, 8.0.0, 7.0.1, 7.0.0

No, it's not 7.0.1, it is 7.1.0. The info on the site is irrelevant(and wrong in this case, too), it's not part of the actual package manager, it's just a website. Apparently, looks like they have wrong info on their website, which is not to be confused with actual package manager: npm. You cannot download imagemin of version 7.0.1 from npm, as I shown above on the screenshot. When you enter what versions are available, there is no 7.0.1:
cmd_IwFyxicaJd

And you can't download it either, you'll get the error.

@fiznool
Copy link

fiznool commented Dec 10, 2021

@KulaGGin that npm information is for the gulp-imagemin package. This repo is for the imagemin package.

@stephengardner
Copy link

2023 here - going back to version 7.0.1 works. Cheers

@AshotAleqsanyan96
Copy link

I don't know if you guys figured out the solution, but I just changed the file extension from js to mjs and replace the require with import, and then everything works fine with version 8.
So something like this.
webp.mjs

import imagemin from 'imagemin';
import imageminWebp from "imagemin-webp";

imagemin(["./images/*.{jpg,png}"], {
	destination: "./images/webp/",
	plugins: [
		imageminWebp({
			//   quality: 90
			//   ,
			//   resize: {
			//     width: 1000,
			//     height: 0
			//   }
		}),
	],
}).then(() => {
	console.log("Images Converted Successfully!!!");
});

and run node .\webp.mjs

Your solution won't work in Typescript

This solution works for me. Thank you very mich!!!

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