Skip to content

Commit

Permalink
esm: throw ERR_REQUIRE_ESM instead of ERR_INTERNAL_ASSERTION
Browse files Browse the repository at this point in the history
PR-URL: #54868
Fixes: #54773
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
aduh95 committed Sep 13, 2024
1 parent c268a35 commit 297bd24
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,6 @@ class ModuleLoader {
* @returns {ModuleJobBase}
*/
getModuleJobForRequire(specifier, parentURL, importAttributes) {
assert(getOptionValue('--experimental-require-module'));

const parsed = URLParse(specifier);
if (parsed != null) {
const protocol = parsed.protocol;
Expand All @@ -346,6 +344,9 @@ class ModuleLoader {
}

const { url, format } = resolveResult;
if (!getOptionValue('--experimental-require-module')) {
throw new ERR_REQUIRE_ESM(url, true);
}
const resolvedImportAttributes = resolveResult.importAttributes ?? importAttributes;
let job = this.loadCache.get(url, resolvedImportAttributes.type);
if (job !== undefined) {
Expand Down
12 changes: 12 additions & 0 deletions test/es-module/test-typescript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ test('execute a TypeScript file with imports', async () => {
strictEqual(result.code, 0);
});

test('execute a TypeScript file with imports', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--eval',
`assert.throws(() => require(${JSON.stringify(fixtures.path('typescript/ts/test-import-fs.ts'))}), {code: 'ERR_REQUIRE_ESM'})`,
]);

strictEqual(result.stderr, '');
strictEqual(result.stdout, '');
strictEqual(result.code, 0);
});

test('execute a TypeScript file with imports with default-type module', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
Expand Down

0 comments on commit 297bd24

Please sign in to comment.