Zero dependency library to capture and parse stack traces in Node, Bun, Deno and more.
Install package:
# npm
npm install errx
# pnpm
pnpm install errximport { captureRawStackTrace, captureStackTrace, parseError, parseRawStackTrace } from 'errx'
// returns raw string stack trace
captureRawStackTrace()
// returns parsed stack trace
captureStackTrace()
// parses the stack of an existing error (empty array if it has none)
parseError(error)
// parses a stack trace string
parseRawStackTrace(error.stack)
console.log(captureStackTrace())
// [{
// function: undefined,
// source: 'file:///code/danielroe/errx/playground/index.js',
// line: 5,
// column: 13
// }]interface ParsedTrace {
column?: number
function?: string
line?: number
source: string
isAsync?: boolean
isConstructor?: boolean
isEval?: boolean
isNative?: boolean
raw?: string
}function holds the bare function name; V8's async and new prefixes are surfaced as isAsync and isConstructor instead. Frames with no resolvable location (at Array.map (<anonymous>), at moduleEvaluation (native)) are kept, with isNative set and no line/column. For eval frames (at eval (eval at fn (file.js:1:2), <anonymous>:3:4)), isEval is set and source/line/column point at the innermost real file location rather than the position inside the evaluated code.
Absolute filesystem paths are normalised to file:// URLs, including Windows drive paths (C:\x\y.js becomes file:///C:/x/y.js) and UNC paths (\\server\share\x.js becomes file://server/share/x.js). Sources that already have a scheme are left untouched.
The is* flags are only present when true. raw holds the original stack trace line. Lines that are recognisably frames but whose shape cannot be parsed are still returned, with an empty source, so they can be rendered from raw.
- Clone this repository
- Enable Corepack using
corepack enable - Install dependencies using
pnpm install - Run interactive tests using
pnpm dev
Made with ❤️
Published under MIT License.