sindresorhus / gulp-imagemin

Minify PNG, JPEG, GIF and SVG images
MIT License
1.9k stars 157 forks source link

I have a question #356

Closed bingheng-wlm closed 2 years ago

bingheng-wlm commented 3 years ago

When I am using gulp and gulp-imagemin with Hexo on GitHub Action, there is an error.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/runner/work/Blog/Blog/node_modules/gulp-imagemin/index.js require() of ES modules is not supported. require() of /home/runner/work/Blog/Blog/node_modules/gulp-imagemin/index.js from /home/runner/work/Blog/Blog/gulpfile.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 /home/runner/work/Blog/Blog/node_modules/gulp-imagemin/package.json.

at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at Object.<anonymous> (/home/runner/work/Blog/Blog/gulpfile.js:6:16)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12) {

code: 'ERR_REQUIRE_ESM' } Error: Process completed with exit code 1.

I don't know why, maybe it is because the index.js in this module?

deavy commented 3 years ago

Same here. Installed version of gulp-imagemin@8.0.0. Downgrade to v7.1.0 solve the problem.

Full error log here:

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

    at new NodeError (node:internal/errors:329:5)
    at Module._extensions..js (node:internal/modules/cjs/loader:1109:13)
    at Object.newLoader [as .js] (/Users/--/--/--/node_modules/pirates/lib/index.js:104:7)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:996:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at Object.<anonymous> (/Users/--/--/--/gulpfile.babel.js:19:1)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Module._compile (/Users/--/--/--/node_modules/pirates/lib/index.js:99:24) {
  code: 'ERR_REQUIRE_ESM'
}
saltfish233 commented 3 years ago

Me too.

`Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: E:\桌面\WEB\BLOG\node_modules_gulp-imagemin@8.0.0@gulp-imagemin\index.js require() of ES modules is not supported. require() of E:\桌面\WEB\BLOG\node_modules_gulp-imagemin@8.0.0@gulp-imagemin\index.js from E:\桌面\WEB\BLOG\gulpfile.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 E:\桌面\WEB\BLOG\node_modules_gulp-imagemin@8.0.0@gulp-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> (E:\桌面\WEB\BLOG\gulpfile.js:2: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' }`

figure002 commented 3 years ago

Same issue here after upgrade from gulp-imagemin 7.1.0 to 8.0.0.

Patta commented 3 years ago

Just read the release notes before upgrade to a new major version. Since 8.0.0 this package is ESM.

figure002 commented 3 years ago

Indeed, the syntax needs to be updated:

// Old syntax
//const imagemin = require('gulp-imagemin');

// New syntax
import imagemin from 'gulp-imagemin';

But then you must use that syntax for all imports and gulpfile.js must be made a module.

zapalagrzegorz commented 3 years ago

@figure002 gulpfile.js must be made a module. , would you minding giving a tip what about this?

agragregra commented 3 years ago

@figure002 gulpfile.js must be made a module. , would you minding giving a tip what about this?

https://github.com/agragregra/OptimizedHTML-5/blob/master/package.json https://github.com/agragregra/OptimizedHTML-5/blob/master/gulpfile.js

figure002 commented 3 years ago

@figure002 gulpfile.js must be made a module. , would you minding giving a tip what about this?

Read these: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c https://gulpjs.com/docs/en/getting-started/javascript-and-gulpfiles/#transpilation