mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
7cb3a662da
When a ESM module cannot be loaded by require due to the presence of TLA, its module status would be stopped at kInstantiated. In this case, when it's imported again, we should allow it to be evaluated asynchronously, as it's also a common pattern for users to retry with dynamic import when require fails. PR-URL: https://github.com/nodejs/node/pull/55502 Fixes: https://github.com/nodejs/node/issues/55500 Refs: https://github.com/nodejs/node/issues/52697 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
27 lines
799 B
JavaScript
27 lines
799 B
JavaScript
// This tests that after loading a ESM with import() and then retrying
|
|
// with require(), it errors as expected, and produces consistent results.
|
|
'use strict';
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
|
|
let ns;
|
|
async function test() {
|
|
const newNs = await import('../fixtures/es-modules/tla/export-async.mjs');
|
|
if (ns === undefined) {
|
|
ns = newNs;
|
|
} else {
|
|
// Check that re-evalaution is returning the same namespace.
|
|
assert.strictEqual(ns, newNs);
|
|
}
|
|
assert.strictEqual(ns.hello, 'world');
|
|
|
|
assert.throws(() => {
|
|
require('../fixtures/es-modules/tla/export-async.mjs');
|
|
}, {
|
|
code: 'ERR_REQUIRE_ASYNC_MODULE'
|
|
});
|
|
}
|
|
|
|
// Run the test twice to check consistency after caching.
|
|
test().then(common.mustCall(test)).catch(common.mustNotCall());
|