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

Unable to run demo using nodejs #31

Open
PLQin opened this issue Jun 22, 2021 · 1 comment
Open

Unable to run demo using nodejs #31

PLQin opened this issue Jun 22, 2021 · 1 comment

Comments

@PLQin
Copy link

PLQin commented Jun 22, 2021

have a js file (demo.js) :

const imagemin = require('imagemin');
const imageminOptipng = require('imagemin-optipng');

(async () => {
	await imagemin(['img/*.png'], 'build/img', {
		use: [
			imageminOptipng()
		]
	});

	console.log('Images optimized!');
})();

and package.json :

{
  "name": "imagemin",
  "version": "1.0.0",
  "description": "",
  "dependencies": {
    "imagemin": "^8.0.0",
    "imagemin-jpegtran": "^7.0.0",
    "imagemin-optipng": "^8.0.0",
    "imagemin-pngquant": "^9.0.2",
    "jpegtran-bin": "^5.0.2"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

run demo.js :

node demo.js

result :

C02F93TLMD6M:imagemin PLQin$ node demo.js 
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/PLQin/Desktop/AtHome/demo/imagemin/node_modules/imagemin/index.js
require() of ES modules is not supported.
require() of /Users/PLQin/Desktop/AtHome/demo/imagemin/node_modules/imagemin/index.js from /Users/PLQin/Desktop/AtHome/demo/imagemin/demo.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/PLQin/Desktop/AtHome/demo/imagemin/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/PLQin/Desktop/AtHome/demo/imagemin/demo.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'
}

What should I do? I just want to use nodejs to compress my pictures (all the pictures in the directory).

Is there any other tool to recommend?

@jordanj-daycos
Copy link

You can downgrade imagemin to v7.0.1, or follow the directions found here.

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