The missing toolkit for parsing, normalizing, and manipulating JavaScript stack traces across all runtimes.
nstack provides utilities to read error stacks, extract method and file information, perform adaptive lookups, and normalize stack traces in Node.js, browsers, Deno, or Bun.
- Parse raw error stacks into structured
StackFrameobjects. - Extract method names, file paths, line numbers, and columns from stack traces.
- Lookup methods in stacks with optional offsets and predicates.
- Adaptive lookups based on the current runtime.
- Provides both raw TypeScript and bundled JS versions (
lib.js,lib.min.js,lib.full.js). - Compatible with ESM, CJS, and TypeScript projects.
npm install "@monitext/nstack"or using yarn:
yarn add "@monitext/nstack"import nstack, { lookUp, adaptiveLookUp, StackUtils, StackLine } from "@monitext/nstack";
try {
// Some code that throws
throw new Error("Oops!");
} catch (err) {
// Parse stack
const stack = StackUtils.processError(err);
// Find a method in the stack
const result = lookUp({ err, method: /myFunction/, offset: 0 });
console.log(result);
// Adaptive lookup across runtime
const adaptive = adaptiveLookUp([
{ mode: "method", err, method: /myFunction/, offset: 0, runtime: "node" },
{ mode: "index", err, index: 1, runtime: "browser" }
]);
console.log(adaptive);
}Represents a single line/frame in a stack trace.
const line = new StackLine("at myFunction (file.ts:10:5)");
console.log(line.method); // "myFunction"
console.log(line.file); // "file.ts"
console.log(line.line); // 10
console.log(line.column); // 5Utilities for processing stacks.
-
StackUtils.processError(error: Error): StackTrace | nullConverts an error’s stack into an array ofStackLine. -
StackUtils.findMethodInStack(param: StackReadParameter): StackResult | nullFinds a method in a stack trace with an optional offset.
lookUp({ err: Error, method: string | RegExp, offset: number }): StackResult | nullSearch for a method in a stack trace. Returns [index, StackLine] or null.
adaptiveLookUp(lookups: AdaptiveLookUp): StackResult | nullIterates over multiple lookups, detects the current runtime, and returns the first successful match.
Supports optional predicates to validate results before returning.
StackFrame– Single stack frame (instance ofStackLine).StackTrace– Array ofStackFrame.StackResult–[index: number, StackFrame].LookUpParameter,StackReadParameter– Parameters for lookups.LookUp,AdaptiveLookUp– Lookup configurations.
- Node.js
- Browser
- Deno
- Bun
adaptiveLookUp automatically selects the proper runtime.
- TypeScript:
dist/src(raw.ts) - JS Bundles:
dist/src-js
| File | Description |
|---|---|
lib.js |
Standard ESM bundle |
lib.full.js |
Full bundle with all dependencies |
lib-min.js |
Minified production bundle |
Apache 2.0