Full answer
Browsers run the output of a build, not the source. After minification the whole app can be on one line with short names, so a stack trace such as at a (app.min.js:1:4821) says little.
A source map (.map file, JSON) records, for each position in the output, the original file, line, column and name. A comment at the end of the output file, //# sourceMappingURL=app.min.js.map, tells tools where to find it. DevTools loads it when it is open, and shows the original files in the Sources panel. Breakpoints and stack traces then refer to your code.
Options in a build:
sourcemap: 'external'or'linked'writes a separate map file. The output can link to it or not.inlineputs the map into the output file as a data URL. This makes the file much larger. Use it in development only.hiddensource maps write the map without the comment, so that browsers do not load it, and the map can be sent to a monitoring tool.- Maps can include the original source text (
sourcesContent), which makes them self-contained and reveals your code to anyone who gets the file.
Privacy and security: maps show your source. If that is a concern, do not serve them publicly. Upload them to your error tracker, which uses them on the server to translate stack traces, and keep them out of the web root. Obfuscation is not a security measure, so the secrets never belong in front-end code anyway.
Other points: the map must match the exact build, so name or version the files, and upload the map with the same release id. Mismatched maps give wrong lines and are worse than none.
Node can use maps too, with node --enable-source-maps. The script below builds a tiny file with minification and a map, runs it with and without that flag, and shows that the error stack points to app.js:3 only when the map is used.
import { build } from 'esbuild';
import { mkdtempSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { spawnSync } from 'node:child_process';
const dir = mkdtempSync(join(tmpdir(), 'map-'));
writeFileSync(join(dir, 'app.js'),
"function checkout(cart) {\n if (cart.items.length === 0) {\n throw new Error('Cart is empty');\n }\n}\ncheckout({ items: [] });\n");
await build({
entryPoints: [join(dir, 'app.js')], bundle: true, minify: true, sourcemap: 'linked',
outfile: join(dir, 'out.js'), logLevel: 'silent',
});
const run = (flags) => spawnSync('node', [...flags, join(dir, 'out.js')], { encoding: 'utf8' }).stderr;
console.log('stack points to app.js:3 with maps:', run(['--enable-source-maps']).includes('app.js:3'));
console.log('stack points to app.js:3 without maps:', run([]).includes('app.js:3'));Run with Node 24.22.1. The output matched the text in the answer in two runs. Run with Node 24 and esbuild 0.28. The output was identical in two runs. Browser DevTools and an error monitoring service were not used.
Key points
- A source map maps minified code back to original files, lines and names.
- A sourceMappingURL comment points tools to the map.
- Use hidden or uploaded maps in production when the source is private.
- The map must match the exact build and release.
- Inline maps are for development only.
Common mistakes
- Serving full maps publicly for a private code base without thinking about it.
- Uploading a map from a different build than the deployed files.
- Using inline source maps in production, which makes files large.
- Believing minified code hides secrets.
Follow-ups
- How does an error monitoring service use source maps?
- What is the difference between a hidden and an external map?
- Why does a map slow nothing for users who never open DevTools?
Tips
- Describe what a map contains and how tools find it.
- Mention the privacy decision for production.