Skip to content

Commit afabd10

Browse files
aduh95tpoisseau
authored andcommittedNov 21, 2024
esm: throw ERR_REQUIRE_ESM instead of ERR_INTERNAL_ASSERTION
PR-URL: nodejs#54868 Fixes: nodejs#54773 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent ece1637 commit afabd10

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed
 

‎lib/internal/modules/esm/loader.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,6 @@ class ModuleLoader {
326326
* @returns {ModuleJobBase}
327327
*/
328328
getModuleJobForRequire(specifier, parentURL, importAttributes) {
329-
assert(getOptionValue('--experimental-require-module'));
330-
331329
const parsed = URLParse(specifier);
332330
if (parsed != null) {
333331
const protocol = parsed.protocol;
@@ -346,6 +344,9 @@ class ModuleLoader {
346344
}
347345

348346
const { url, format } = resolveResult;
347+
if (!getOptionValue('--experimental-require-module')) {
348+
throw new ERR_REQUIRE_ESM(url, true);
349+
}
349350
const resolvedImportAttributes = resolveResult.importAttributes ?? importAttributes;
350351
let job = this.loadCache.get(url, resolvedImportAttributes.type);
351352
if (job !== undefined) {

‎test/es-module/test-typescript.mjs

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ test('execute a TypeScript file with imports', async () => {
2828
strictEqual(result.code, 0);
2929
});
3030

31+
test('execute a TypeScript file with imports', async () => {
32+
const result = await spawnPromisified(process.execPath, [
33+
'--no-warnings',
34+
'--eval',
35+
`assert.throws(() => require(${JSON.stringify(fixtures.path('typescript/ts/test-import-fs.ts'))}), {code: 'ERR_REQUIRE_ESM'})`,
36+
]);
37+
38+
strictEqual(result.stderr, '');
39+
strictEqual(result.stdout, '');
40+
strictEqual(result.code, 0);
41+
});
42+
3143
test('execute a TypeScript file with imports with default-type module', async () => {
3244
const result = await spawnPromisified(process.execPath, [
3345
'--experimental-strip-types',

0 commit comments

Comments
 (0)