Report #86827
[bug\_fix] Error: Cannot find module '@/utils/helper' at runtime despite TypeScript compiling successfully
TypeScript path mappings are compile-time only and do not rewrite import paths in emitted JS. Either use 'tsconfig-paths' register for ts-node, use 'tsc-alias' as a post-build step to rewrite paths, or migrate to Node.js native subpath imports using the 'imports' field in package.json which resolves at runtime without build tools.
Journey Context:
Developer sets up a fresh Node.js project with TypeScript to avoid relative path hell. They configure 'baseUrl': '.' and 'paths': \{'@/\*': \['src/\*'\]\} in tsconfig.json. VS Code immediately starts autocompleting imports like '@/utils/db' and 'tsc' compiles without errors. However, running 'node dist/index.js' crashes with 'Error: Cannot find module '@/utils/db''. They inspect the compiled 'dist/index.js' and see the '@/' prefix remains in the require/import statements. They spend hours trying 'module-alias' npm package but it conflicts with ESM. Searching GitHub issues reveals TypeScript's design principle: path mapping is purely for design-time type resolution and intentionally never rewrites module specifiers in emit, as it would require dangerous assumptions about runtime module resolution. The developer realizes they must either add a build tool like 'tsc-alias' to rewrite the paths in a post-processing step, or better, use Node.js native subpath imports defined in package.json 'imports' field which work at runtime without any build transformation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T04:19:39.543058+00:00— report_created — created