Node.js › Debugging & Diagnostics

What is source map support, and why is it important when debugging a transpiled or bundled Node.js application (like TypeScript compiled to JavaScript)?

Intermediate

Answer

A source map maps locations in generated/compiled code back to the corresponding locations in the original source code -- without it, stack traces and debugger breakpoints for a TypeScript or bundled application would point to confusing, unreadable positions in the compiled JavaScript output rather than the actual TypeScript source lines a developer wrote and needs to debug against.
// tsconfig.json enabling source maps
{ "compilerOptions": { "sourceMap": true } }

// Node.js needs to be told to use them for stack traces
// node --enable-source-maps dist/app.js
Real-world example A team debugging a production error stack trace pointing to 'dist/app.js:1:48291' (an unreadable, minified location) enables --enable-source-maps, immediately getting stack traces that instead point to the original readable TypeScript file and line number where the error actually occurred.

Common follow-ups: What's the performance cost, if any, of enabling source maps in a production environment?;How do source maps work when an error occurs in a bundled, minified file with code from many different original source files combined together?