V8 Stack Trace Manipulation
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
-
TypeError: Cannot read properties of undefined (reading 'toString') (or similar for CallSite methods)
cause A stack modifier function (either `extend` or `filter`) returned something other than an array of V8 `CallSite` objects, or modified the objects in an unexpected way.fixReview your `modifier` functions. Ensure they always return an array, and that array contains valid `CallSite` objects. Do not mutate `CallSite` objects in ways that remove essential properties if they are expected by subsequent processors. -
Error: `Error.stack` is not formatted as expected after calling `chain.format.replace`
cause Another library or a custom setup is also modifying `Error.stack`, or `Error.stack` was already accessed on the error object before `chain.format.replace` was called.fixVerify that `stack-chain` is the last or only library modifying `Error.stack` at the global level. Ensure `chain.format.replace` is called early in your application's lifecycle, before `Error.stack` is likely to be accessed for the first time on relevant error objects. -
ReferenceError: require is not defined (when using `require('stack-chain')` in an ESM module)cause Attempting to use CommonJS `require` syntax directly within an ECMAScript Module (ESM) context in Node.js.fixConvert your module to CommonJS by using `.js` extension with `"type": "commonjs"` in `package.json`, or rename your file to `.cjs`. Alternatively, if you must use ESM, you can try dynamic `import('stack-chain')` or a build step to transpile, but direct compatibility is not guaranteed for an abandoned package.
Warnings
- gotcha Modifying `Error.stack` globally can lead to conflicts with other libraries or tools that also attempt to manipulate stack traces, causing unpredictable behavior or race conditions. Use with caution in shared environments.
- gotcha Calling `chain.format.restore()` does not guarantee that `Error.stack` will revert to its original V8 format for all existing `Error` objects. If `Error.stack` or `Error.callSite` has already been accessed on an `Error` object, its value is cached and will not change. Only newly created errors or errors whose stack hasn't been accessed will reflect the restored formatter.
- breaking The project is abandoned (last commit 2017, last publish 2014) and may not be compatible with modern Node.js versions (e.g., >Node 8-10) or recent V8 engine changes. APIs it relies on (V8 Stack Trace API) might have evolved or been removed, leading to unexpected behavior or crashes.
- gotcha When using `chain.filter.attach` or `chain.extend.attach`, your modifier function *must* return a modified `frames` array. Failing to return an array, or returning `null` or `undefined`, can break subsequent modifiers or the stack trace generation process, leading to unexpected errors or incomplete stack traces.
Install
-
npm install stack-chain -
yarn add stack-chain -
pnpm add stack-chain
Imports
- chain
import chain from 'stack-chain';
const chain = require('stack-chain'); - chain.extend.attach
import { extend } from 'stack-chain'; extend.attach(modifier);const chain = require('stack-chain'); chain.extend.attach(modifier); - chain.format.replace
const chain = require('stack-chain'); chain.format.replace(formatter);
Quickstart
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);