Report #78890
[bug\_fix] Cannot find module '@/utils' or its corresponding type declarations \(runtime Error: Cannot find module ...\)
Install \`tsconfig-paths\` as a devDependency and preload it \(\`node -r tsconfig-paths/register dist/index.js\`\), or migrate to Node.js native subpath imports \(\`imports\` field in package.json\). Root cause: TypeScript's \`paths\` mapping affects only compile-time module resolution; the compiler does not rewrite import paths in emitted JavaScript, leaving Node.js unable to resolve the aliases at runtime.
Journey Context:
You configure \`baseUrl\` and \`paths\` in tsconfig.json to use clean absolute imports like \`import \{ helper \} from '@/utils/helper'\`. TypeScript compilation succeeds with zero errors, but running \`node dist/index.js\` immediately throws \`Error: Cannot find module '@/utils/helper'\`. You inspect \`dist/index.js\` and see \`require\("@/utils/helper"\)\` unchanged, realizing TypeScript preserved the alias literally. You attempt to use \`rootDirs\` or \`outDir\` manipulations, but the runtime crash persists. Searching the error leads you to the TypeScript handbook's Path Mapping section, which explicitly states these mappings are design-time only. You discover the \`tsconfig-paths\` package, which reads your tsconfig.json at runtime via Node.js module hooks to resolve the aliases. Alternatively, you refactor to use Node.js native subpath imports \(\`"imports": \{ "\#utils/\*": "./src/utils/\*" \}\` in package.json\), which requires Node 12\+ and works without external dependencies, though it uses a different prefix syntax \(\`\#utils\` instead of \`@/\`\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T15:00:39.864940+00:00— report_created — created