Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1876,9 +1876,15 @@ function markNodeModules(ctx, line) {
tempLine += StringPrototypeSlice(line, lastPos, moduleStart);

let moduleEnd = StringPrototypeIndexOf(line, separator, moduleStart);
if (line[moduleStart] === '@') {
if (moduleEnd === -1) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny nit: we could make it a nested if statement to have one less comparison

@Ijtihed Ijtihed Jun 20, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks!

// No trailing separator: the module name runs to the end of the line.
moduleEnd = line.length;
} else if (line[moduleStart] === '@') {
// Namespaced modules have an extra slash: @namespace/package
moduleEnd = StringPrototypeIndexOf(line, separator, moduleEnd + 1);
if (moduleEnd === -1) {
moduleEnd = line.length;
}
}

const nodeModule = StringPrototypeSlice(line, moduleStart, moduleEnd);
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3418,6 +3418,21 @@ assert.strictEqual(
);
}

{
// A node_modules segment that is the last path component (no trailing
// separator after the module name) must not send markNodeModules into an
// infinite loop that exhausts the heap.
// https://github.com/nodejs/node/issues/64011
const err = new Error('boom');
err.stack = 'Error: boom\n at /app/node_modules/foo.js:1:1';
const out = util.inspect(err, { colors: true });
assert.strictEqual(
out,
'Error: boom\n' +
' at /app/node_modules/\x1B[4mfoo.js:1:1\x1B[24m',
);
}

{
// Cross platform checks.
const err = new Error('foo');
Expand Down