Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ class ModuleJob extends ModuleJobBase {
const parentFilename = urlToFilename(parent?.filename);
this.module.hasAsyncGraph ??= this.module.isGraphAsync();

if (this.module.async && !getOptionValue('--experimental-print-required-tla')) {
if (this.module.hasAsyncGraph && !getOptionValue('--experimental-print-required-tla')) {
throw new ERR_REQUIRE_ASYNC_MODULE(filename, parentFilename);
}
if (status === kInstantiated) {
Expand Down
21 changes: 21 additions & 0 deletions test/es-module/test-import-require-tla-twice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
// This tests that in the require() in imported CJS can retry loading an ESM with TLA
// twice and get the correct error both times.

require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
const fixtures = require('../common/fixtures');
const assert = require('assert');

spawnSyncAndAssert(
process.execPath,
['--import', fixtures.fileURL('es-modules', 'import-require-tla-twice', 'hook.js'),
fixtures.path('es-modules', 'import-require-tla-twice', 'require-tla.js'),
],
{
stdout(output) {
const matches = output.matchAll(/e\.code === ERR_REQUIRE_ASYNC_MODULE true/g);
assert.strictEqual([...matches].length, 2);
}
}
);
6 changes: 6 additions & 0 deletions test/fixtures/es-modules/import-require-tla-twice/hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { registerHooks } = require('module');
registerHooks({
load(url, context, nextLoad) {
return nextLoad(url, context);
}
});
11 changes: 11 additions & 0 deletions test/fixtures/es-modules/import-require-tla-twice/require-tla.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
try {
require('./tla.mjs');
} catch (e) {
console.log('e.code === ERR_REQUIRE_ASYNC_MODULE', e.code === 'ERR_REQUIRE_ASYNC_MODULE');
}

try {
require('./tla.mjs');
} catch (e) {
console.log('e.code === ERR_REQUIRE_ASYNC_MODULE', e.code === 'ERR_REQUIRE_ASYNC_MODULE');
}
1 change: 1 addition & 0 deletions test/fixtures/es-modules/import-require-tla-twice/tla.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
await Promise.resolve('1');
Loading