Report #56131
[bug\_fix] Cannot find module '@app/components' or its corresponding type declarations at runtime despite successful TypeScript compilation \(MODULE\_NOT\_FOUND\)
Configure Node.js native subpath imports in package.json using the 'imports' field \(requires TypeScript 4.7\+ with 'moduleResolution': 'node16' or 'bundler'\), or use the 'tsconfig-paths' package to rewrite paths at runtime. The root cause is that tsc emits compiled code with the original path strings and does not rewrite path aliases to relative paths; Node.js then fails to resolve the bare specifier because it only understands standard Node resolution or package.json 'imports'.
Journey Context:
You configure path mapping in tsconfig.json with 'baseUrl': '.' and 'paths': \{'@app/\*': \['./src/\*'\]\}. VS Code resolves imports perfectly, and 'tsc --noEmit' reports no errors. However, running 'node dist/main.js' immediately throws 'Error: Cannot find module '@app/components''. You verify the compiled output in 'dist/' and see 'require\('@app/components'\)' exactly as written, realizing TypeScript never rewrote the alias to a relative path like '../components'. You search for solutions and find recommendations for 'module-alias' or 'tsconfig-paths'. You try installing 'tsconfig-paths' and running 'node -r tsconfig-paths/register dist/main.js', which works because the package reads tsconfig.json and intercepts Node's require to resolve the mapped paths dynamically. Alternatively, you refactor to use Node.js native subpath imports in package.json: '"imports": \{"\#app/\*": "./src/\*"\}', change imports to '\#app/components', set 'moduleResolution': 'node16' in tsconfig, and everything works at both compile and runtime without external tools because TypeScript 4.7\+ understands the Node.js 'imports' field and resolves it correctly during type-checking, while Node.js resolves it at runtime.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T00:42:35.040107+00:00— report_created — created