diff --git a/src/cjs/api/module-resolve-filename/index.ts b/src/cjs/api/module-resolve-filename/index.ts index 9e88026a2..46454a54d 100644 --- a/src/cjs/api/module-resolve-filename/index.ts +++ b/src/cjs/api/module-resolve-filename/index.ts @@ -78,7 +78,13 @@ export const createResolveFilename = ( nextResolveSimple = createTsExtensionResolver( nextResolveSimple, - Boolean(parent?.filename && tsExtensionsPattern.test(parent.filename)), + Boolean( + // If register.namespace is used (e.g. tsx.require()) + namespace + + // If parent is a TS file + || (parent?.filename && tsExtensionsPattern.test(parent.filename)), + ), ); nextResolveSimple = createImplicitResolver(nextResolveSimple); diff --git a/tests/specs/api.ts b/tests/specs/api.ts index 4bfb85614..edb02ee20 100644 --- a/tests/specs/api.ts +++ b/tests/specs/api.ts @@ -2,6 +2,7 @@ import path from 'node:path'; import { execaNode } from 'execa'; import { testSuite, expect } from 'manten'; import { createFixture } from 'fs-fixture'; +import { outdent } from 'outdent'; import { tsxCjsPath, tsxCjsApiPath, @@ -11,7 +12,6 @@ import { type NodeApis, } from '../utils/tsx.js'; import { createPackageJson, createTsconfig, expectErrors } from '../fixtures.js'; -import { outdent } from 'outdent'; const tsFiles = { 'file.ts': outdent` @@ -266,7 +266,8 @@ export default testSuite(({ describe }, node: NodeApis) => { ...tsFiles, }); - const { stdout } = await execaNode(fixture.getPath('require.cjs'), [], { + const { stdout } = await execaNode('./require.cjs', [], { + cwd: fixture.path, nodePath: node.path, nodeOptions: [], });