module: use sync cjs when importing cts

PR-URL: https://github.com/nodejs/node/pull/60072
Fixes: https://github.com/nodejs/node/issues/59963
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
This commit is contained in:
Marco Ippolito 2025-10-02 05:49:26 +02:00 committed by GitHub
parent 23b834058c
commit 69144e96c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 38 additions and 1 deletions

View File

@ -504,7 +504,9 @@ class ModuleLoader {
const loadResult = this.#loadSync(url, { format, importAttributes }); const loadResult = this.#loadSync(url, { format, importAttributes });
// Use the synchronous commonjs translator which can deal with cycles. // Use the synchronous commonjs translator which can deal with cycles.
const finalFormat = loadResult.format === 'commonjs' ? 'commonjs-sync' : loadResult.format; const finalFormat =
loadResult.format === 'commonjs' ||
loadResult.format === 'commonjs-typescript' ? 'commonjs-sync' : loadResult.format;
if (finalFormat === 'wasm') { if (finalFormat === 'wasm') {
assert.fail('WASM is currently unsupported by require(esm)'); assert.fail('WASM is currently unsupported by require(esm)');

View File

@ -174,3 +174,13 @@ test('expect failure of a .cts file requiring esm in node_modules', async () =>
match(result.stderr, /ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING/); match(result.stderr, /ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING/);
strictEqual(result.code, 1); strictEqual(result.code, 1);
}); });
test('cts -> require mts -> import cts', async () => {
const result = await spawnPromisified(process.execPath, [
fixtures.path('typescript/cts/issue-59963/a.cts'),
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, 'Hello from c.cts\n');
strictEqual(result.code, 0);
});

View File

@ -98,3 +98,14 @@ test('execute .ts file importing a module', async () => {
strictEqual(result.stdout, 'Hello, TypeScript!\n'); strictEqual(result.stdout, 'Hello, TypeScript!\n');
strictEqual(result.code, 0); strictEqual(result.code, 0);
}); });
test('mts -> import cts -> require mts', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
fixtures.path('typescript/mts/issue-59963/a.mts'),
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, 'Hello from c.mts\n');
strictEqual(result.code, 0);
});

View File

@ -0,0 +1,3 @@
const { message } = require("./b.mts");
interface Foo {};
console.log(message);

View File

@ -0,0 +1,2 @@
interface Foo {};
export { message } from "./c.cts";

View File

@ -0,0 +1,2 @@
const message: string = "Hello from c.cts";
module.exports = { message };

View File

@ -0,0 +1,3 @@
import { message } from "./b.cts";
interface Foo {};
console.log(message);

View File

@ -0,0 +1,3 @@
const { message } = require("./c.mts");
interface Foo {};
module.exports = { message };

View File

@ -0,0 +1 @@
export const message: string = "Hello from c.mts";