Report #74419
[bug\_fix] Runtime module resolution failure for tsconfig path aliases: 'Error: Cannot find module @app/shared' at runtime despite successful TypeScript compilation
TypeScript path mapping is compile-time only and does not rewrite emitted JavaScript. Node.js \(or the runtime\) does not read tsconfig.json. Fix by using 'tsconfig-paths' \(install as devDependency and preload with 'node -r tsconfig-paths/register dist/index.js'\), configure the bundler \(Webpack/Vite/esbuild\) to resolve aliases during build, or migrate to Node.js native subpath imports \(package.json 'imports' field\) which both TypeScript and Node.js resolve natively without external tools.
Journey Context:
You configure a monorepo with 'packages/shared' containing utilities and map '@app/shared' to '../packages/shared/src' in tsconfig.json paths. 'tsc --build' succeeds, but running 'node dist/apps/api/index.js' throws 'Error: Cannot find module '@app/shared''. Inspecting the compiled JS shows 'require\("@app/shared"\)' - the path alias was preserved literally. You realize TypeScript never rewrites import paths; it only validates types. You search for solutions and discover that tsconfig-paths provides a runtime hook to resolve these aliases, or that switching to Node.js native subpath imports \(package.json 'imports'\) avoids this tooling gap entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T07:30:41.855515+00:00— report_created — created