V8 Stack Trace Manipulation

2.0.0 · abandoned · verified Wed Apr 22

stack-chain is a utility library for Node.js that provides an API to intercept, modify, and reformat V8 JavaScript engine's stack traces. It allows developers to programmatically extend, filter, and replace the default stack trace formatting behavior of `Error.stack`. This is particularly useful for tools that need to clean up stack traces (e.g., hide internal frames in frameworks), provide custom error reporting, or integrate with debugging utilities. The current stable version is 2.0.0. The project appears to be abandoned, with the last publish over 8 years ago and last commit in 2017. This means it may not be compatible with newer Node.js versions or modern JavaScript features like ESM, and new releases or bug fixes are highly unlikely. Its key differentiator was offering a centralized, hook-based system for manipulating global `Error.stack` behavior.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to attach a stack frame filter and replace the global stack trace formatter, then trigger and print an error.

const chain = require('stack-chain');

// Attach a filter to remove frames originating from this file
chain.filter.attach(function (error, frames) {
    const filteredFrames = frames.filter(function (callSite) {
        return callSite.getFileName() !== module.filename;
    });
    return filteredFrames;
});

// Replace the stack formatter to add a custom prefix
chain.format.replace(function (error, frames) {
    let lines = [];
    lines.push(`CUSTOM ERROR: ${error.toString()}`);
    for (let i = 0; i < frames.length; i++) {
        lines.push(`    at (custom) ${frames[i].toString()}`);
    }
    return lines.join('\n');
});

function myFunction() {
    const err = new Error('Something went wrong!');
    console.log(err.stack);
}

myFunction();

// Restore default V8 formatter after some time (note gotcha on restore)
setTimeout(() => {
  chain.format.restore();
  console.log('\nRestored default stack format, but existing errors may not reflect it.');
  const err2 = new Error('Another error after restore');
  console.log(err2.stack);
}, 100);

view raw JSON →