Report #79781
[bug\_fix] TS2307: Cannot find module '@/utils' or its corresponding type declarations.
Install \`tsconfig-paths\` as a devDependency and register it at runtime by adding the Node.js require flag: \`node -r tsconfig-paths/register dist/index.js\` \(or \`ts-node -r tsconfig-paths/register src/index.ts\`\). Root cause: TypeScript path mapping \(compilerOptions.paths\) is used only by the compiler for type-checking; it does not rewrite import paths in the emitted JavaScript, so the Node.js runtime module resolver \(which knows nothing about tsconfig.json\) fails to find the alias.
Journey Context:
A developer refactors a codebase to use path aliases to avoid relative import hell \(e.g., \`../../../utils\`\). They add \`"@/\*": \["./src/\*"\]\` to tsconfig.json compilerOptions.paths and baseUrl. VS Code autocomplete works perfectly, and \`tsc --noEmit\` reports no errors. However, running the app with \`ts-node src/index.ts\` throws "Error: Cannot find module '@/utils'". The developer checks the compiled output in \`dist/\` and sees the import statement is still \`require\('@/utils'\)\`, unchanged. They search the error and discover that TypeScript never rewrites module specifiers. They find the \`tsconfig-paths\` package, which reads the tsconfig.json at runtime and patches Node's \`Module.\_resolveFilename\` to honor the paths. After installing it and modifying their start script to include \`-r tsconfig-paths/register\`, the application boots successfully. They now understand the distinction between compile-time type resolution and runtime module resolution.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T16:30:37.685592+00:00— report_created — created