Skip to content

The use of top level await preventing gulp-imagemin from working on node.js version after 22.12.0 #394

@maple3142

Description

@maple3142

package.json:

{
  "name": "gulpimagemin",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "gulp": "^5.0.0",
    "gulp-imagemin": "^9.1.0"
  }
}

gulpfile.mjs:

import 'gulp-imagemin'

When using node.js 22.11.0 it works fine:

> node --version
v22.11.0
> yarn gulp --tasks
yarn run v1.22.22
$ /home/maple3142/workspace/gulpimagemin/node_modules/.bin/gulp --tasks
Tasks for ~/workspace/gulpimagemin/gulpfile.mjs
Done in 0.21s.

But in node.js 22.12.0 it errors:

> node --version
v22.12.0
> NODE_OPTIONS=--experimental-print-required-tla yarn gulp --tasks
yarn run v1.22.22
$ /home/maple3142/workspace/gulpimagemin/node_modules/.bin/gulp --tasks
Error: unexpected top-level await at file:///home/maple3142/workspace/gulpimagemin/node_modules/gulp-imagemin/index.js:96
export const gifsicle = await exposePlugin('gifsicle');
                        ^



Error: require() cannot be used on an ESM graph with top-level await. Use import() instead. To see where the top-level await comes from, use --experimental-print-required-tla.
    at ModuleJobSync.runSync (node:internal/modules/esm/module_job:395:35)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:329:47)
    at Object.loadESMFromCJS [as .mjs] (node:internal/modules/cjs/loader:1414:24)
    at Module.load (node:internal/modules/cjs/loader:1318:32)
    at Function._load (node:internal/modules/cjs/loader:1128:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:219:24)
    at Module.require (node:internal/modules/cjs/loader:1340:12)
    at require (node:internal/modules/helpers:138:16)
    at requireOrImport (/home/maple3142/workspace/gulpimagemin/node_modules/gulp-cli/lib/shared/require-or-import.js:20:11) {
  code: 'ERR_REQUIRE_ASYNC_MODULE'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

The main difference is that node.js 22.12.0 is the first version that enabled require(esm) (without top level await) from cjs module by default, but it somehow broke gulp + gulp-imagemin for some reason. Not sure how to fix it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions