What are Source Maps?
Source maps are files that map compiled JavaScript code back to the original TypeScript code. This allows you to see the original TypeScript code in error stack traces and debuggers. Usage:- View original TypeScript locations in error stack traces
- Set breakpoints in TypeScript code in debuggers
- Map production errors to original code
Enabling Source Maps
tsconfig.json Configuration
dist/JS
index.jsMAP
index.js.map - Source map filemodels/JS
user.model.jsMAP
user.model.js.mapinlineSourceMap vs sourceMap
Error Stack Traces
Without Source Maps
With Source Maps
Using Source Maps in Node.js
—enable-source-maps Flag
package.json:
Using tsx (Recommended)
VSCode Debugger Configuration
launch.json
Debugging Steps
-
Set breakpoints
- Click to the left of the line number in TypeScript files (
.ts)
- Click to the left of the line number in TypeScript files (
-
Start debugger
- Press F5 or run from the “Run and Debug” panel
-
Inspect variables
- When paused at a breakpoint, examine variable values
- Evaluate expressions in the Watch panel
-
Step execution
- F10: Step Over (next line)
- F11: Step Into (inside function)
- Shift+F11: Step Out (outside function)
Production Source Maps
Security Considerations
Integration with Error Tracking Services
Upload source maps to error tracking services like Sentry:Troubleshooting Source Maps
1. Source Maps Not Generated
Check:2. Stack Trace Still Points to .js Files
Cause: Node.js is not recognizing source maps Solution:3. VSCode Debugger Not Working
Check:- Reset
.vscode/launch.json - Restart TypeScript server:
Cmd+Shift+P→ “TypeScript: Restart TS Server” - Restart debugger
4. Incorrect Line Numbers
Cause: Source maps are out of sync with code Solution:Validating Source Maps
Manual Validation
Validation with source-map Package
Source Maps in the Browser
Vite Development Server
Vite automatically generates and serves source maps:Chrome DevTools
-
Open Sources tab
- F12 → Sources tab
-
Find TypeScript files
- Look for
.tsfiles underwebpack://orsrc/
- Look for
-
Set breakpoints
- Click on line numbers in TypeScript files
-
Debug
- Refresh the page to hit breakpoints
Best Practices
1. Always Enable in Development
2. Exclude .map Files from Git
3. Selective Use in Production
4. Error Monitoring Integration
In production, provide source maps only to error tracking services:Related Tools
1. source-map-support
Automatically apply source maps at runtime:2. @esbuild-kit/core-utils
Source map support for esbuild-based build tools:3. Chrome DevTools
- Map compiled CSS to original SCSS/LESS in Elements tab
- Check source map downloads in Network tab
- Click original file links in Console