Source Map Support
History
Node.js supports TC39 ECMA-426 Source Map format (it was called Source map revision 3 format).
The APIs in this section are helpers for interacting with the source map cache. This cache is populated when source map parsing is enabled and source map include directives are found in a modules' footer.
To enable source map parsing, Node.js must be run with the flag
--enable-source-maps, or with code coverage enabled by setting
NODE_V8_COVERAGE=dir, or be enabled programmatically via
module.setSourceMapsSupport().
// module.mjs // In an ECMAScript module import { findSourceMap, SourceMap } from 'node:module';
// module.cjs // In a CommonJS module const { findSourceMap, SourceMap } = require('node:module');
module.getSourceMapsSupport(): Object
This method returns whether the Source Map v3 support for stack traces is enabled.
module.findSourceMap(path): module.SourceMap | undefined
stringmodule.SourceMap | undefinedmodule.SourceMap if a source
map is found, undefined otherwise.path is the resolved path for the file for which a corresponding source map
should be fetched.
module.setSourceMapsSupport(enabled, options?): void
This function enables or disables the Source Map v3 support for stack traces.
It provides same features as launching Node.js process with commandline options
--enable-source-maps, with additional options to alter the support for files
in node_modules or generated codes.
Only source maps in JavaScript files that are loaded after source maps has been
enabled will be parsed and loaded. Preferably, use the commandline options
--enable-source-maps to avoid losing track of source maps of modules loaded
before this API call.
new SourceMap(payload, { lineLengths }?): SourceMap
Creates a new sourceMap instance.
payload is an object with keys matching the Source map format:
lineLengths is an optional array of the length of each line in the
generated code.
ObjectGetter for the payload used to construct the SourceMap instance.
sourceMap.findEntry(lineOffset, columnOffset): Object
Given a line offset and column offset in the generated source file, returns an object representing the SourceMap range in the original file if found, or an empty object if not.
The object returned contains the following keys:
numbernumberstringnumbernumberstringThe returned value represents the raw range as it appears in the SourceMap, based on zero-indexed offsets, not 1-indexed line and column numbers as they appear in Error messages and CallSite objects.
To get the corresponding 1-indexed line and column numbers from a
lineNumber and columnNumber as they are reported by Error stacks
and CallSite objects, use sourceMap.findOrigin(lineNumber, columnNumber)
sourceMap.findOrigin(lineNumber, columnNumber): Object
Given a 1-indexed lineNumber and columnNumber from a call site in
the generated source, find the corresponding call site location
in the original source.
If the lineNumber and columnNumber provided are not found in any
source map, then an empty object is returned. Otherwise, the
returned object contains the following keys: