Skip to content

Node 22 ERR_REQUIRE_ASYNC_MODULE if gulpfile.mjs contains top-level await #268

Open
@56789a1987

Description

@56789a1987

Description

Node now allows using require() to import ES modules, ERR_REQUIRE_ESM is not triggered.
But if the module containes top-level await, ERR_REQUIRE_ASYNC_MODULE is triggered and it's not handled.

The following gulpfile.mjs contains top-level await.
In Node 20, this works.
In Node 22, this throws ERR_REQUIRE_ASYNC_MODULE.

Sample of gulpfile

import { readFile } from 'fs/promises';
const packageJson = await readFile('package.json', 'utf8');
export function test(cb) {
    console.log(packageJson);
    cb();
}
export default test;

Terminal output

$ gulp
Error [ERR_REQUIRE_ASYNC_MODULE]: 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:392:13)
    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/***/.nvm/versions/node/v22.12.0/lib/node_modules/gulp/node_modules/gulp-cli/lib/shared/require-or-import.js:20:11) {
  code: 'ERR_REQUIRE_ASYNC_MODULE'
}

Version:

  • node: v22.12.0
  • npm: v10.9.0
  • gulp: CLI version: 3.0.0, Local version: 5.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions