Universal JavaScript Stack Trace Library

2.0.2 · active · verified Sun Apr 19

stacktrace.js is a framework-agnostic micro-library designed to generate, parse, and enhance JavaScript stack traces across all environments, including browsers and Node.js. The current stable version is 2.0.2. It maintains a moderate release cadence, with updates typically addressing dependency bumps, bug fixes, and minor enhancements. Key differentiators include its ability to parse ES6 code, extensible StackFrame objects that report on constructor, native, or eval code, and its use of source maps for enhanced trace accuracy. It modularizes functionality into several sub-projects like error-stack-parser and stacktrace-gps to handle specific aspects of stack trace processing, providing both asynchronous (Promise-based) and synchronous API methods for flexibility. The library is particularly useful for robust error reporting and debugging applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates asynchronous stack trace capture from a new Error object and the current execution context, as well as a synchronous trace.

import { get, fromError } from 'stacktrace-js';

async function captureAndLogStackTraces() {
  try {
    // Simulate an error to capture its stack
    let simulatedError = new Error('This is a simulated error for demonstration.');
    const stackframesFromError = await fromError(simulatedError);
    console.log('--- Stack trace from Error object ---');
    console.log(stackframesFromError.map(sf => sf.toString()).join('\n'));

    // Get the stack trace from the current execution point
    const stackframesCurrent = await get();
    console.log('\n--- Stack trace from current location ---');
    console.log(stackframesCurrent.map(sf => sf.toString()).join('\n'));

  } catch (err: any) {
    console.error('Failed to get stack trace:', err.message);
  }
}

captureAndLogStackTraces();

// Example of synchronous usage (without source map resolution)
import { getSync } from 'stacktrace-js';
function callSynchronousTrace() {
  console.log('\n--- Synchronous Stack trace (no source maps) ---');
  const syncStackframes = getSync();
  console.log(syncStackframes.map(sf => sf.toString()).join('\n'));
}
callSynchronousTrace();

view raw JSON →