Commit 1d069afb538 for nodejs
commit 1d069afb5385364a94a0874557dd040dfefdc75c
Author: Chengzhong Wu <legendecas@gmail.com>
Date: Wed Nov 25 17:57:49 2026 +0800
lib: skip source maps in node_modules by default
Signed-off-by: Chengzhong Wu <cwu631@bloomberg.net>
PR-URL: https://github.com/nodejs/node/pull/67627
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
diff --git a/doc/api/cli.md b/doc/api/cli.md
index 1decf37bdc2..6a8b3756fe7 100644
--- a/doc/api/cli.md
+++ b/doc/api/cli.md
@@ -1273,6 +1273,9 @@ is permitted.
<!-- YAML
added: v12.12.0
changes:
+ - version: REPLACEME
+ pr-url: https://github.com/nodejs/node/pull/67627
+ description: Source maps in `node_modules` are skipped by default.
- version:
- v15.11.0
- v14.18.0
@@ -1287,6 +1290,9 @@ application reference the transpiled code, not the original source position.
`--enable-source-maps` enables caching of Source Maps and makes a best
effort to report stack traces relative to the original source file.
+Source maps of files in `node_modules` are skipped. Use
+[`module.setSourceMapsSupport()`][] to enable source maps for them.
+
Overriding `Error.prepareStackTrace` may prevent `--enable-source-maps` from
modifying the stack trace. Call and return the results of the original
`Error.prepareStackTrace` in the overriding function to modify the stack trace
@@ -5051,6 +5057,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
[`dnsPromises.lookup()`]: dns.md#dnspromiseslookuphostname-options
[`import.meta.url`]: esm.md#importmetaurl
[`inspector.open()`]: inspector.md#inspectoropenport-host-wait
+[`module.setSourceMapsSupport()`]: module.md#modulesetsourcemapssupportenabled-options
[`net.getDefaultAutoSelectFamilyAttemptTimeout()`]: net.md#netgetdefaultautoselectfamilyattempttimeout
[`node:ffi`]: ffi.md
[`node:sqlite`]: sqlite.md
diff --git a/doc/api/module.md b/doc/api/module.md
index c8ad7818a52..6d72f450bf4 100644
--- a/doc/api/module.md
+++ b/doc/api/module.md
@@ -1271,7 +1271,7 @@ This function enables or disables the [Source Map v3][Source Map] 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
+`--enable-source-maps`, with additional options to enable the support for files
in `node_modules` or generated codes.
Only source maps in JavaScript files that are loaded after source maps has been
diff --git a/doc/node.1 b/doc/node.1
index e7cd3f40514..05fc5418e47 100644
--- a/doc/node.1
+++ b/doc/node.1
@@ -777,6 +777,8 @@ When using a transpiler, such as TypeScript, stack traces thrown by an
application reference the transpiled code, not the original source position.
\fB--enable-source-maps\fR enables caching of Source Maps and makes a best
effort to report stack traces relative to the original source file.
+Source maps of files in \fBnode_modules\fR are skipped. Use
+\fBmodule.setSourceMapsSupport()\fR to enable source maps for them.
Overriding \fBError.prepareStackTrace\fR may prevent \fB--enable-source-maps\fR from
modifying the stack trace. Call and return the results of the original
\fBError.prepareStackTrace\fR in the overriding function to modify the stack trace
diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js
index b5933f4e9d6..960118c4228 100644
--- a/lib/internal/process/pre_execution.js
+++ b/lib/internal/process/pre_execution.js
@@ -804,12 +804,9 @@ function initializeSourceMapsHandlers() {
setSourceMapsSupport,
} = require('internal/source_map/source_map_cache');
const enabled = getOptionValue('--enable-source-maps');
+ // Source maps in node_modules are skipped by default.
setSourceMapsSupport(enabled, {
__proto__: null,
- // TODO(legendecas): In order to smoothly improve the source map support,
- // skip source maps in node_modules and generated code with
- // `--enable-source-maps` in a semver major version.
- nodeModules: enabled,
generatedCode: enabled,
});
}
diff --git a/test/fixtures/source-map/output/source_map_disabled_by_api.js b/test/fixtures/source-map/output/source_map_disabled_by_api.js
index 1291f3583ac..d131f8f5349 100644
--- a/test/fixtures/source-map/output/source_map_disabled_by_api.js
+++ b/test/fixtures/source-map/output/source_map_disabled_by_api.js
@@ -9,7 +9,7 @@ Error.stackTraceLimit = 5;
assert.deepStrictEqual(Module.getSourceMapsSupport(), {
__proto__: null,
enabled: true,
- nodeModules: true,
+ nodeModules: false,
generatedCode: true,
});
Module.setSourceMapsSupport(false);
diff --git a/test/fixtures/source-map/output/source_map_enable_source_maps_defaults.js b/test/fixtures/source-map/output/source_map_enable_source_maps_defaults.js
new file mode 100644
index 00000000000..2a0a9fefc07
--- /dev/null
+++ b/test/fixtures/source-map/output/source_map_enable_source_maps_defaults.js
@@ -0,0 +1,21 @@
+// Flags: --enable-source-maps
+
+'use strict';
+require('../../../common');
+const assert = require('node:assert');
+const Module = require('node:module');
+Error.stackTraceLimit = 2;
+
+// --enable-source-maps skips source maps in node_modules.
+assert.deepStrictEqual(Module.getSourceMapsSupport(), {
+ __proto__: null,
+ enabled: true,
+ nodeModules: false,
+ generatedCode: true,
+});
+
+try {
+ require('../node_modules/error-stack/enclosing-call-site-min.js').simpleErrorStack();
+} catch (e) {
+ console.log(e);
+}
diff --git a/test/fixtures/source-map/output/source_map_enable_source_maps_defaults.snapshot b/test/fixtures/source-map/output/source_map_enable_source_maps_defaults.snapshot
new file mode 100644
index 00000000000..520fbb4d01d
--- /dev/null
+++ b/test/fixtures/source-map/output/source_map_enable_source_maps_defaults.snapshot
@@ -0,0 +1,3 @@
+Error: an error!
+ at functionD (<project-root>/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:156)
+ at functionC (<project-root>/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:97)
diff --git a/test/parallel/test-node-output-sourcemaps.mjs b/test/parallel/test-node-output-sourcemaps.mjs
index c96eac2bd03..9a0ec43bac4 100644
--- a/test/parallel/test-node-output-sourcemaps.mjs
+++ b/test/parallel/test-node-output-sourcemaps.mjs
@@ -9,6 +9,7 @@ describe('sourcemaps output', { concurrency: !process.env.TEST_PARALLEL }, () =>
{ name: 'source-map/output/source_map_assert_ok_no_source_map.cjs' },
{ name: 'source-map/output/source_map_disabled_by_api.js' },
{ name: 'source-map/output/source_map_disabled_by_process_api.js' },
+ { name: 'source-map/output/source_map_enable_source_maps_defaults.js' },
{ name: 'source-map/output/source_map_enabled_by_api.js' },
{ name: 'source-map/output/source_map_enabled_by_api_node_modules.js' },
{ name: 'source-map/output/source_map_enabled_by_process_api.js' },