esm: throw ERR_REQUIRE_ESM instead of ERR_INTERNAL_ASSERTION

PR-URL: https://github.com/nodejs/node/pull/54868
Fixes: https://github.com/nodejs/node/issues/54773
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Antoine du Hamel 2024-09-11 23:48:02 +02:00 committed by GitHub
parent cc780ddf1e
commit 53cdffea20
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 2 deletions

View File

@ -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;
@ -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) {

View File

@ -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',