Files
node/test/es-module/test-require-module-tla-error-metadata.js
Joyee Cheung a006c5dd12 esm: improve ERR_REQUIRE_ASYNC_MODULE
This brings back several improvements that were reverted by mistake when
landing https://redirect.github.com/nodejs/node/pull/64154

- Update the documentation about how the removal of side effects of
  source collection
- Add non-enumerable `requireStack` and `topLevelAwaitLocations`
  properties to `ERR_REQUIRE_ASYNC_MODULE`, the latter is only
  populated when --experimental-print-required-tla is enabled
- Add "Required module: <url>" to the error message to identify the
  required ESM entry point regardless of whether the flag is enabled
- Fix TLA caret column from 0-based to 1-based
- Store module source via a private symbol instead of a public property
- Use `hasAsyncGraph` (post-instantiation) in `throwIfAsyncGraph`
  instead of walking the graph before instantiation
- Merge the require stack checking into the
  `common.expectRequiredTLAError` helper.
- Removed tests that are made redundant by the snapshot tests

Signed-off-by: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64260
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2026-07-04 15:22:48 +00:00

27 lines
1.0 KiB
JavaScript

// Flags: --experimental-print-required-tla
'use strict';
// Tests that ERR_REQUIRE_ASYNC_MODULE carries metadata about the failure
// when --experimental-print-required-tla is enabled.
require('../common');
const assert = require('assert');
const fixtures = require('../common/fixtures');
const entrypoint = fixtures.path('es-modules/tla/require-indented.js');
const url = fixtures.fileURL('es-modules/tla/tla-indented.mjs').href;
assert.throws(() => {
require(entrypoint);
}, (err) => {
assert.strictEqual(err.code, 'ERR_REQUIRE_ASYNC_MODULE');
assert.deepStrictEqual(err.requireStack, [entrypoint, __filename]);
assert.deepStrictEqual(err.topLevelAwaitLocations, [
{ __proto__: null, url, line: 4, column: 3, sourceLine: ' await Promise.resolve(ready);' },
{ __proto__: null, url, line: 7, column: 1, sourceLine: 'for await (const x of [Promise.resolve(1)]) {' },
]);
// The properties are not enumerable so they do not show up in the inspected error.
assert.deepStrictEqual(Object.keys(err), ['code']);
return true;
});