Map minified code back with source maps
Show original TypeScript lines in stack traces with node --enable-source-maps and a tsc sourceMap build, instead of line numbers from compiled JavaScript.
A stack trace points at dist/app.js:14, a line of compiled output you never wrote. A source map links that line back to your original file, and Node.js can use it when asked.
function main() {
throw new Error("boom");
}
try {
main();
} catch (err) {
console.log((err as Error).stack);
}npx tsc --sourceMap --outDir dist src/app.ts
node --enable-source-maps dist/app.jsError: boom
at main (/app/src/app.ts:2:9)
at Object.<anonymous> (/app/src/app.ts:6:3)The compiler writes dist/app.js.map, and the flag makes Node.js read it when it builds the stack, so the frames show src/app.ts with the right line and column. Node.js prints a few internal frames after these, which are left out here. See the [--enable-source-maps flag](https://nodejs.org/api/cli.html#--enable-source-maps).
Without the flag, the same run reports the compiled dist/app.js lines. Browsers do the same mapping in DevTools when a .map file is reachable.
The flag adds some overhead when errors are created, so it is best for development and for services where you want readable traces. Keep the maps next to the build, or upload them to your error tracker.