Registry / observability / stack-chain

stack-chain

JSON →
library2.0.0jsnpmunverified

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.

npm install stack-chain
INSTALL
IMPORT
SIG · STACK-CHAIN
S
stack-chain
observabilityjavascriptv2.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

chain
const chain = require('stack-chain');
import chain from 'stack-chain';
This library is primarily CommonJS. While a transpiler might allow `import`, direct ESM support is not provided, and `require` is the intended usage.
chain.extend.attach
const chain = require('stack-chain'); chain.extend.attach(modifier);
import { extend } from 'stack-chain'; extend.attach(modifier);
The main export is a single 'chain' object with nested methods; individual functions are not directly exportable as named imports.
chain.format.replace
const chain = require('stack-chain'); chain.format.replace(formatter);
This method replaces the global V8 stack formatter, allowing custom string output for `Error.stack`.

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);
Debug
Known issues
gotchaModifying `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.
fix
Thoroughly test `stack-chain` in your environment, especially if other global error handlers or debugging tools are in use. Consider encapsulating its use or only enabling it in specific execution contexts.
affects: >=1.0.0
gotchaCalling `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.
fix
Be aware of the caching behavior of V8 `Error` objects. If you need to ensure a restored format, primarily apply `restore()` before new errors are generated or before their stacks are accessed.
affects: >=1.0.0
breakingThe 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.
fix
For new projects or existing projects on recent Node.js versions, consider alternative, actively maintained stack manipulation libraries or native V8 APIs directly. If using this package, thorough compatibility testing with your specific Node.js version is essential.
affects: >=2.0.0
gotchaWhen 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.
fix
Always ensure your modifier function for `attach` methods explicitly returns an array of `callSite` objects, even if it's the original, unmodified array.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'toString') (or similar for CallSite methods)
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.
fix
Review 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`
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.
fix
Verify 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)
Attempting to use CommonJS `require` syntax directly within an ECMAScript Module (ESM) context in Node.js.
fix
Convert 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.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
16
OpenAI (training)
1
Resources