Report #50435
[bug\_fix] Cannot find module '@/components/Button' at runtime despite TypeScript compiling successfully
Install the \`tsconfig-paths\` package and preload it via \`node -r tsconfig-paths/register dist/index.js\`, or configure your bundler \(Vite/Webpack\) to resolve the alias at build time. For Node.js native ESM, use subpath imports in package.json instead. Root cause: TypeScript \`paths\` remapping is purely a compile-time construct for type resolution; the emitted JavaScript retains the original bare specifier or alias, which Node.js or browsers cannot resolve without additional tooling.
Journey Context:
Developer configures \`compilerOptions.paths\` in tsconfig.json to map \`@/\*\` to \`src/\*\` to avoid relative path hell. VS Code IntelliSense works perfectly, and \`tsc --noEmit\` reports zero errors. They compile the project and run \`node dist/server.js\`, immediately crashing with "Error: Cannot find module '@/utils/db'". Inspecting \`dist/server.js\`, they see \`require\("@/utils/db"\)\` unchanged. Confused, they search StackOverflow and discover the distinction between TypeScript's module resolution and Node.js runtime resolution. They initially try changing the import to relative paths, which works but defeats the purpose. They then find \`tsconfig-paths\`, install it, and preload the registration script. The runtime now reads the tsconfig.json and resolves the paths dynamically, matching the compile-time behavior.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T15:08:28.112381+00:00— report_created — created