Report #12571
[bug\_fix] Cannot find module '@/utils/helper' or its corresponding type declarations. \(TS2307\)
Use \`tsc-alias\` to rewrite path aliases in the emitted JavaScript after \`tsc\` compilation, or preload \`tsconfig-paths/register\` when running the code \(e.g., \`node -r tsconfig-paths/register dist/index.js\`\). Alternatively, use a bundler like Vite or Webpack that resolves aliases at build time. The root cause is that TypeScript's \`paths\` configuration is only for compile-time type resolution; the compiler does not rewrite import paths in the output JavaScript, leaving Node.js unable to resolve the alias at runtime.
Journey Context:
Developer sets up a Node.js project with TypeScript and defines \`paths\` in \`tsconfig.json\` \(e.g., \`"@utils/\*": \["src/utils/\*"\]\`\) to avoid \`../../../\` relative path hell. VS Code IntelliSense works perfectly, and \`tsc\` compiles without errors. However, running \`node dist/index.js\` immediately crashes with \`Error: Cannot find module '@utils/helper'\`. The developer spends hours verifying that \`baseUrl\` is correct, trying different path patterns like \`./src/utils/\*\` vs \`src/utils/\*\`, and checking if \`rootDir\` affects it. They eventually realize that the compiled \`.js\` files in \`dist/\` still contain \`require\('@utils/helper'\)\`, which Node.js doesn't understand. Searching online, they discover that TypeScript intentionally does not touch import paths during emit. The solution involves adding \`tsc-alias\` to their build pipeline \(\`tsc && tsc-alias\`\) to physically rewrite the strings in the output files, or using \`tsconfig-paths\` to patch the Node.js module resolution at runtime.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T16:19:39.055763+00:00— report_created — created