StackTracey is a robust JavaScript library designed for parsing call stacks, reading source code, and generating clean, filtered, and pretty-printed output. Currently at version 2.2.0, it targets both Node.js and browser environments, supporting cross-platform compatibility across various operating systems like Windows and *nix. The library differentiates itself with full sourcemap support, the ability to fetch source text for call locations (leveraging `get-source`), and mechanisms for ad-hoc exclusion of irrelevant stack frames (e.g., library calls or user-defined exclusions via `// @hide` markers). It provides both synchronous and asynchronous interfaces for accessing source code, with asynchronous being the preferred method for browser environments to avoid blocking the main thread. It also extracts useful information from `SyntaxError` instances, making it valuable for debugging and enhanced error reporting in development tools and logging solutions.
npm install stacktraceyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to capture and parse an error stack, access individual stack frame properties, asynchronously fetch source code for frames, and finally pretty-print the entire processed stack. It highlights the library's core capabilities for error introspection and rich output.
Prefer `await stack.withSourcesAsync()` for fetching source code. This method returns a Promise and performs I/O operations without blocking the event loop, making it suitable for both browser and Node.js environments.
Always use `import StackTracey from 'stacktracey'` for modern JavaScript and TypeScript projects. If forced to use CommonJS, dynamic import `(await import('stacktracey')).default` might be necessary, or ensure your build system correctly transpiles ESM imports.Always check for the existence of `item.sourceFile` and `item.sourceFile.text` before attempting to access its properties to prevent runtime errors. Ensure sourcemaps are correctly generated and accessible for deployed code to maximize source fetching capabilities.
Use `const StackTracey = require('stacktracey').default;` or, preferably, migrate your code to use ESM imports: `import StackTracey from 'stacktracey';`.Always `await` the result of `stack.withSourcesAsync()` to ensure the Promise resolves and errors are caught: `const stackWithSources = await stack.withSourcesAsync();`.