Map generated code back to original sources
Quick answer
A source map relates positions in generated code to positions in original source. It helps a debugger show readable original files even when the running JavaScript is bundled or transformed.
How it works
The generated script executes; the map is diagnostic metadata, not an alternate implementation. A sourceMappingURL comment can refer to an external map or inline data URL. A version 3 map includes sources and encoded mappings; sourcesContent may embed original source. DevTools can load the map to display original locations. Node’s node:module SourceMap class can query mappings explicitly; in findEntry positions are zero-based. Publish maps only with an intentional access policy because they may reveal source. The fixtures query a small explicit map; the DevTools UI display remains manual.
Local examples
Run in Node.js v22.16.0 as a CommonJS script. No interactive debugger attached; actual ByteMind editor support is unverified.
(async () => {const {SourceMap}=require("node:module");const map=new SourceMap({version:3,sources:["original.js"],names:[],mappings:"AAAA"});const entry=map.findEntry(0,0);console.log(entry.originalLine,entry.originalColumn,entry.originalSource.endsWith("original.js"));})()Expected console output:
0 0 trueAAAA maps generated position zero to original position zero.
Run in Node.js v22.16.0 as a CommonJS script. No interactive debugger attached; actual ByteMind editor support is unverified.
(async () => {const map={version:3,sources:["price.js"],sourcesContent:["export const price = 2;"],names:[],mappings:"AAAA"};console.log(map.version,map.sources[0],map.sourcesContent[0].includes("price"));})()Expected console output:
3 price.js trueEmbedded source can be visible to anyone who receives the map.
Common mistakes
Do not assume maps fix runtime bugs or trust stale maps from a different build.
Practice
Compare a generated line with its mapped original in the source-map lab; verify that the map belongs to the exact build.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Map generated code back to original sources — Knowledge Check
Apply the concept and reason about local example output.
Which code actually executes when a bundle has a source map?
Why should source map distribution be reviewed?
What coordinate system does Node SourceMap.findEntry use?
What is the exact console output in Map generated code back to original sources example 1?
(async () => {const {SourceMap}=require("node:module");const map=new SourceMap({version:3,sources:["original.js"],names:[],mappings:"AAAA"});const entry=map.findEntry(0,0);console.log(entry.originalLine,entry.originalColumn,entry.originalSource.endsWith("original.js"));})()What is the exact console output in Map generated code back to original sources example 2?
(async () => {const map={version:3,sources:["price.js"],sourcesContent:["export const price = 2;"],names:[],mappings:"AAAA"};console.log(map.version,map.sources[0],map.sourcesContent[0].includes("price"));})()