Report #39467
[bug\_fix] Runtime Error: Cannot find module '@/components/Button' imported from /dist/index.js, despite TypeScript compilation succeeding
Install \`tsconfig-paths\` and preload it via \`node -r tsconfig-paths/register dist/index.js\`, or configure your bundler \(Vite/Webpack/esbuild\) to resolve the paths during build. Alternatively, replace tsconfig paths with Node.js native subpath imports \(package.json \`imports\` field\) which resolve at runtime.
Journey Context:
Developer sets up path mapping in tsconfig.json \(\`"@/\*": \["./src/\*"\]\`\) to clean up imports and avoid \`../../../\` relative paths. The TypeScript language service works perfectly—no red squiggles, IntelliSense resolves imports, and \`tsc\` emits JavaScript without errors. However, running \`node dist/index.js\` immediately crashes with "Error: Cannot find module '@/components/Button'". Inspecting \`dist/index.js\` reveals \`require\("@/components/Button"\)\`—the path alias was never rewritten. The developer realizes TypeScript's \`paths\` are purely for compile-time type resolution and declaration emit; they do not affect emitted JS module specifiers. After consulting the TypeScript handbook, they understand that runtime module resolution is delegated entirely to Node.js, which knows nothing about tsconfig.json. They implement \`tsconfig-paths\` as a runtime loader to intercept Node's module resolution, or migrate to native Node.js subpath imports which are respected by both TypeScript \(via moduleResolution: bundler or node16\+\) and Node.js runtime.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T20:43:21.684464+00:00— report_created — created