Fix ERR_MODULE_NOT_FOUND for TypeScript/ESM imports#1942
Conversation
## 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 finished @mohammedahmed18's task in 3m 2s —— View job PR Review Summary
Prek ChecksAll Code ReviewFix is correct for the primary use case. TypeScript files ( One potential bug — # project_root_from_lang is computed at line 2195 but NOT used here:
module_system = detect_module_system(project_root, source_file) # uses original argFor Minor: Duplicate DetectionNo duplicates detected. Last updated: 2026-04-01 |
|
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. |
Problem
Codeflash was generating import statements without file extensions for TypeScript and ESM projects, causing
ERR_MODULE_NOT_FOUNDerrors when Node.js tried to resolve the modules.Example Error
From trace
08d0e99e-10e6-4ad2-981d-b907e3c068ea:The generated test had:
But Node.js ESM requires explicit file extensions.
Root Cause
The
get_module_pathmethod inJavaScriptSupportwas unconditionally removing file extensions with.with_suffix(""), regardless of whether the project used ESM or CommonJS module system.Solution
Modified
get_module_pathto:detect_module_system().jsextension (TypeScript convention)TypeScript convention: Use
.jsextension in imports even when the source file is.ts, as imports reference the compiled output. This is required for Node.js ESM resolution.Testing
TestGetModulePathclassuv run prek)Files Changed
codeflash/languages/javascript/support.py- Fixedget_module_pathmethodtests/test_languages/test_javascript_support.py- Added regression testsRelated Issues
Fixes the issue seen in multiple Nest.js optimization runs where generated tests failed with
ERR_MODULE_NOT_FOUND.