diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 45f685c71ad028..0ac3ed855a6278 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -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) { diff --git a/test/es-module/test-typescript.mjs b/test/es-module/test-typescript.mjs index 0a713a36e42ace..f8b79261495638 100644 --- a/test/es-module/test-typescript.mjs +++ b/test/es-module/test-typescript.mjs @@ -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',