Skip to content

Fix ERR_MODULE_NOT_FOUND for TypeScript/ESM imports#1942

Merged
Saga4 merged 1 commit intomainfrom
fix/esm-module-path-extension
Apr 1, 2026
Merged

Fix ERR_MODULE_NOT_FOUND for TypeScript/ESM imports#1942
Saga4 merged 1 commit intomainfrom
fix/esm-module-path-extension

Conversation

@mohammedahmed18
Copy link
Copy Markdown
Contributor

Problem

Codeflash was generating import statements without file extensions for TypeScript and ESM projects, causing ERR_MODULE_NOT_FOUND errors when Node.js tried to resolve the modules.

Example Error

From trace 08d0e99e-10e6-4ad2-981d-b907e3c068ea:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module
'/workspace/target/packages/microservices/server/server-factory'
imported from .../test_create__unit_test_0.test.ts

The generated test had:

import ServerFactory from '../../server/server-factory'

But Node.js ESM requires explicit file extensions.

Root Cause

The get_module_path method in JavaScriptSupport was unconditionally removing file extensions with .with_suffix(""), regardless of whether the project used ESM or CommonJS module system.

Solution

Modified get_module_path to:

  1. Detect the module system using detect_module_system()
  2. For ESM or TypeScript files: add .js extension (TypeScript convention)
  3. For CommonJS: keep no extension (backward compatible)

TypeScript convention: Use .js extension in imports even when the source file is .ts, as imports reference the compiled output. This is required for Node.js ESM resolution.

Testing

  • ✅ Added two new test cases in TestGetModulePath class
  • ✅ All 73 existing JavaScript support tests pass
  • ✅ All 28 module system tests pass
  • ✅ Lint and type checks pass (uv run prek)

Files Changed

  • codeflash/languages/javascript/support.py - Fixed get_module_path method
  • tests/test_languages/test_javascript_support.py - Added regression tests

Related Issues

Fixes the issue seen in multiple Nest.js optimization runs where generated tests failed with ERR_MODULE_NOT_FOUND.

## Problem
Codeflash was generating import statements without file extensions for
TypeScript and ESM projects, causing ERR_MODULE_NOT_FOUND errors when
Node.js tried to resolve the modules.

Example error from trace 08d0e99e-10e6-4ad2-981d-b907e3c068ea:
```
Error [ERR_MODULE_NOT_FOUND]: Cannot find module
'/workspace/target/packages/microservices/server/server-factory'
imported from .../test_create__unit_test_0.test.ts
```

The generated test had:
```typescript
import ServerFactory from '../../server/server-factory'
```

But Node.js ESM requires explicit file extensions.

## Root Cause
The get_module_path method in JavaScriptSupport was unconditionally
removing file extensions with .with_suffix(""), regardless of whether
the project used ESM or CommonJS module system.

## Solution
Modified get_module_path to:
1. Detect the module system using detect_module_system()
2. For ESM or TypeScript files: add .js extension (TypeScript convention)
3. For CommonJS: keep no extension (backward compatible)

TypeScript convention is to use .js extension in imports even when the
source file is .ts, as imports reference the compiled output.

## Testing
- Added two new test cases in TestGetModulePath class
- All 73 existing JavaScript support tests pass
- All 28 module system tests pass
- Lint and type checks pass

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@claude
Copy link
Copy Markdown
Contributor

claude bot commented Apr 1, 2026

Claude finished @mohammedahmed18's task in 3m 2s —— View job


PR Review Summary

  • Triage PR size — SMALL (43 lines of production code changed)
  • Lint and type checks — prek passes; mypy errors are pre-existing
  • Resolve stale review threads — none found
  • Code review
  • Duplicate detection
  • Post summary

Prek Checks

All ruff check and ruff format checks pass. Mypy shows 32 pre-existing errors in support.py (e.g., TypeScriptSupport registration error) — none introduced by this PR.

Code Review

Fix is correct for the primary use case. TypeScript files (.ts, .tsx, .mts) are identified by extension in detect_module_system, so they correctly receive the .js suffix regardless of package.json. CommonJS behavior is preserved. The change is backward-compatible.

One potential bugdetect_module_system is called with the original project_root argument, not the resolved project_root_from_lang derived a few lines earlier (support.py:2212):

# project_root_from_lang is computed at line 2195 but NOT used here:
module_system = detect_module_system(project_root, source_file)  # uses original arg

For .js files in an ESM monorepo where package.json lives at project_root_from_lang (not at the passed-in project_root), the ESM detection would fall through to file-content analysis or default to CommonJS, and the .js extension wouldn't be appended. TypeScript files are unaffected (extension-based detection). Fix this →

Minor: ModuleSystem is an informal string-constant class rather than enum.Enum. Not introduced by this PR, so not blocking.

Duplicate Detection

No duplicates detected. get_module_path exists separately in python/support.py, javascript/support.py, and java/support.py — these are intentional per-language implementations dispatched via the LanguageSupport protocol.


Last updated: 2026-04-01

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Codeflash Bot seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@Saga4 Saga4 merged commit 5baccd4 into main Apr 1, 2026
27 of 32 checks passed
@Saga4 Saga4 deleted the fix/esm-module-path-extension branch April 1, 2026 15:21
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.

3 participants