Report #103448
[bug\_fix] Cannot find module './utils' or its corresponding type declarations. ts\(2307\)
When 'moduleResolution' is 'NodeNext' or 'Node16' and the project is ESM \('type': 'module'\), write imports with the '.js' extension even for '.ts' source files: 'import \{ foo \} from './utils.js''. TypeScript resolves the '.js' extension to the corresponding '.ts' file at compile time. If a bundler handles resolution, set 'moduleResolution': 'bundler' instead.
Journey Context:
After migrating a Node project from CommonJS to ESM, imports like 'import \{ foo \} from './utils'' failed at runtime with 'ERR\_MODULE\_NOT\_FOUND'. I added '.js' extensions to fix Node, but then 'tsc' complained it could not find './utils.js'. I changed 'moduleResolution' from 'node' \(which is the legacy Node.js CJS algorithm\) to 'NodeNext'. Under 'NodeNext', TypeScript's resolver emulates Node ESM and understands that a '.js' import maps to a '.ts' source file. The root cause is that legacy 'node' resolution predates ESM and ignores the '.js'->'.ts' mapping, while ESM runtimes require explicit extensions.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-11T04:25:14.635499+00:00— report_created — created