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

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

Open
56789a1987 opened this issue Dec 23, 2024 · 1 comment · May be fixed by #269
Open

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

56789a1987 opened this issue Dec 23, 2024 · 1 comment · May be fixed by #269

Comments

@56789a1987
Copy link

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
@epelc
Copy link

epelc commented Dec 31, 2024

Note this was broken in v22.12.0(works in v22.11.0 and earlier). It's due to this https://nodejs.org/en/blog/release/v22.12.0#requireesm-is-now-enabled-by-default

I just submitted a fix to detect the new error code. Stuck on old node version until then.

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

Successfully merging a pull request may close this issue.

2 participants