Skip to main content
This covers effective debugging methods using TypeScript source maps.

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

Generated files:

inlineSourceMap vs sourceMap

Error Stack Traces

Without Source Maps

Only compiled JavaScript locations are shown, making debugging difficult.

With Source Maps

Shows the original TypeScript file and line numbers accurately.

Using Source Maps in Node.js

—enable-source-maps Flag

Or in package.json:
Sonamu uses tsx, so source maps work without additional configuration.

VSCode Debugger Configuration

launch.json

Debugging Steps

  1. Set breakpoints
    • Click to the left of the line number in TypeScript files (.ts)
  2. Start debugger
    • Press F5 or run from the “Run and Debug” panel
  3. Inspect variables
    • When paused at a breakpoint, examine variable values
    • Evaluate expressions in the Watch panel
  4. Step execution
    • F10: Step Over (next line)
    • F11: Step Into (inside function)
    • Shift+F11: Step Out (outside function)

Production Source Maps

Security Considerations

When deploying to production:

Integration with Error Tracking Services

Upload source maps to error tracking services like Sentry:

Troubleshooting Source Maps

1. Source Maps Not Generated

Check:
Solution:

2. Stack Trace Still Points to .js Files

Cause: Node.js is not recognizing source maps Solution:

3. VSCode Debugger Not Working

Check:
Solution:
  1. Reset .vscode/launch.json
  2. Restart TypeScript server: Cmd+Shift+P → “TypeScript: Restart TS Server”
  3. 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

  1. Open Sources tab
    • F12 → Sources tab
  2. Find TypeScript files
    • Look for .ts files under webpack:// or src/
  3. Set breakpoints
    • Click on line numbers in TypeScript files
  4. 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:

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