Report #45082
[bug\_fix] Runtime Error: Cannot find module '@utils/helper' when executing compiled JavaScript in Node.js, despite successful TypeScript compilation and working IntelliSense
TypeScript path mapping \(paths/baseUrl\) is compile-time only for type resolution and does not rewrite module specifiers in emitted JS. The fix is to either: \(1\) Use Node.js native subpath imports \(package.json "imports" field\) which TypeScript 4.7\+ supports with moduleResolution "node16" or "bundler", \(2\) Use a build tool like tsc-alias to rewrite paths post-compilation, or \(3\) Use a runtime loader like tsx that resolves TypeScript paths during execution. The root cause is the fundamental architectural separation between TypeScript's type resolution and Node.js's runtime module resolution algorithm.
Journey Context:
Developer configures path mapping in tsconfig.json: \{"baseUrl": ".", "paths": \{"@/\*": \["./src/\*"\]\}\}. VS Code immediately resolves '@utils/helper' to './src/utils/helper.ts' and provides autocomplete. Running tsc builds successfully with no errors. Developer runs node dist/index.js and hits 'Error: Cannot find module '@utils/helper''. Checking dist/index.js reveals require\('@utils/helper'\) was emitted verbatim. Developer assumes paths affects runtime resolution. They try installing 'module-alias' or configuring webpack \(irrelevant for Node runtime\). They search GitHub issues and discover TypeScript documentation explicitly stating paths is for type-checking only. Realization dawns that Node.js knows nothing about tsconfig. Developer researches solutions: Option A\) Switch to Node.js subpath imports \("imports": \{"\#utils/\*": "./dist/utils/\*"\}\) which is native and requires TypeScript 4.7\+ with moduleResolution "node16". Option B\) Add tsc-alias to build pipeline to rewrite imports post-tsc. Option C\) Use tsx \(Node.js loader\) for development which handles path mapping at runtime. Developer chooses based on deployment constraints, understanding the architectural boundary between compile-time types and runtime module systems.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T06:08:23.596269+00:00— report_created — created