Report #73942
[bug\_fix] Cannot find module '@/\*' or its corresponding type declarations. \(2307\)
Replace TypeScript path mapping with Node.js subpath imports. In package.json, define '"imports": \{ "\#\*": "./dist/\*" \}' and reference '\#/' instead of '@/'. Configure TypeScript with '"moduleResolution": "NodeNext"' or '"Node16"' so it resolves these imports. Alternatively, use a bundler \(Vite, Webpack, esbuild\) that rewrites aliases at build time, or use tsx/ts-node with a loader like 'tsconfig-paths'.
Journey Context:
Developer configures '@/\*' path mapping in tsconfig.json to avoid relative imports \('../../../utils'\). VS Code works perfectly—IntelliSense resolves imports, no red squiggles. Developer runs 'node dist/index.js' \(or uses 'tsx src/index.ts'\) and gets a runtime crash: 'Error: Cannot find module @/utils'. Checking 'dist/index.js', the import statement reads 'require\("@/utils"\)'—TypeScript emitted the alias verbatim without transforming it. The developer discovers that TypeScript path mapping is purely a compile-time type-resolution feature; it never rewrites module specifiers in emitted JS. They try 'tsconfig-paths/register' as a runtime hack, then realize that for Node.js native execution, the robust solution is using Node.js native subpath imports \(package.json 'imports' field\), which TypeScript supports since 4.7 with NodeNext resolution. This fixes the runtime error natively without runtime hacks.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T06:42:32.875171+00:00— report_created — created