Skip to content

Comments

fix: guard parent access in addTsEsmHook for ESM-to-CJS sub-dependency resolution#1087

Open
setthase wants to merge 1 commit intohapijs:masterfrom
setthase:fix-ts-esm-dep-resolve
Open

fix: guard parent access in addTsEsmHook for ESM-to-CJS sub-dependency resolution#1087
setthase wants to merge 1 commit intohapijs:masterfrom
setthase:fix-ts-esm-dep-resolve

Conversation

@setthase
Copy link

What: Adds a null guard for parent in internals.addTsEsmHook() (lib/cli.js).

Why: When running TypeScript tests (--typescript) in an ESM project that imports from an ESM package with CJS sub-dependencies, the crash path is: require.extensions['.ts']Module._compileloadESMFromCJS → ESM translators → CJS sub-dependency resolution → Module._resolveFilename with parent === undefined. The current code accesses parent.filename unconditionally, causing TypeError: Cannot read properties of undefined (reading 'filename').

Fix: Guard the parent access so that when parent is undefined (ESM-to-CJS transition context), the hook falls through to the original _resolveFilename, which handles the resolution natively.

Test: Added test/cli_typescript_esm_dep/ fixture — a TypeScript ESM test that imports from an ESM package which has a CJS sub-dependency (two-layer structure required to trigger the crash). Without the fix, this crashes. With the fix, it passes.

Ref: fixes #1086

@setthase setthase force-pushed the fix-ts-esm-dep-resolve branch from 4ee1b6d to d7a95fa Compare February 20, 2026 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@hapi/lab crashes when TypeScript tests depend on ESM packages

1 participant