Parses call stacks. Reads sources. Clean & filtered output. Sourcemaps. Node & browsers.
Installations
npm install stacktracey
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
14.8.0
NPM Version
6.14.7
Score
99.4
Supply Chain
99.5
Quality
78.2
Maintenance
100
Vulnerability
99.6
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
Download Statistics
Total Downloads
80,706,618
Last Day
212,438
Last Week
1,015,651
Last Month
4,645,651
Last Year
50,542,967
GitHub Statistics
219 Stars
367 Commits
22 Forks
6 Watching
2 Branches
6 Contributors
Bundle Size
37.70 kB
Minified
12.12 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.1.8
Package Id
stacktracey@2.1.8
Unpacked Size
44.15 kB
Size
13.61 kB
File Count
16
NPM Version
6.14.7
Node Version
14.8.0
Total Downloads
Cumulative downloads
Total Downloads
80,706,618
Last day
-10.2%
212,438
Compared to previous day
Last week
-17.5%
1,015,651
Compared to previous week
Last month
6.8%
4,645,651
Compared to previous month
Last year
111.2%
50,542,967
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
StackTracey
Parses call stacks. Reads sources. Clean & filtered output. Sourcemaps. Node & browsers.
Why
- Simple
- Works in Node and browsers, *nix and Windows
- Allows hiding library calls / ad-hoc exclusion (via
// @hide
marker) - Provides source text for call locations
- Fetches sources (via get-source)
- Supports both asynchronous and synchronous interfaces (works even in browsers)
- Full sourcemap support
- Extracts useful information from
SyntaxError
instances - Pretty printing
What For
- Error overlay UIs for easier front-end development
- Better error reporting for Node projects
- Advanced logging (displaying call locations)
- Assertion printing
How To
1npm install stacktracey
1import StackTracey from 'stacktracey'
Captures the current call stack:
1stack = new StackTracey () // captures the current call stack
Parses stacks from an Error
object:
1stack = new StackTracey (error) 2stack = new StackTracey (error.stack) // ...or from raw string
Stores parsed data in .items
:
1stack.items.length // num entries 2stack.items[0] // top
...where each item exposes:
1{ 2 beforeParse: <original text>, 3 callee: <function name>, 4 calleeShort: <shortened function name>, 5 file: <full path to file>, // e.g. /Users/john/my_project/node_modules/foobar/main.js 6 fileRelative: <relative path to file>, // e.g. node_modules/foobar/main.js 7 fileShort: <short path to file>, // e.g. foobar/main.js 8 fileName: <file name>, // e.g. main.js 9 line: <line number>, // starts from 1 10 column: <column number>, // starts from 1 11 12 index: /* true if occured in HTML file at index page */, 13 native: /* true if occured in native browser code */, 14 thirdParty: /* true if occured in library code */, 15 hide: /* true if marked as hidden by "// @hide" tag */, 16 syntaxError: /* true if generated from a SyntaxError instance */ 17}
Accessing sources (synchronously, use with caution in browsers):
1stack = stack.withSources () // returns a copy of stack with all items supplied with sources 2top = stack.items[0] // top item
Accessing sources (asynchronously, preferred method in browsers):
1stack = await stack.withSourcesAsync () // returns a copy of stack with all items supplied with sources 2top = stack.items[0] // top item
...or:
1top = stack.withSourceAt (0) // supplies source for an individiual item (by index)
1top = await stack.withSourceAsyncAt (0) // supplies source for an individiual item (by index)
...or:
1top = stack.withSource (stack.items[0]) // supplies source for an individiual item
1top = await stack.withSourceAsync (stack.items[0]) // supplies source for an individiual item
The returned items contain the following additional fields (already mapped through sourcemaps):
1{ 2 ... // all the previously described fields 3 4 line: <original line number>, 5 column: <original column number>, 6 sourceFile: <original source file object>, 7 sourceLine: <original source line text> 8}
To learn about the sourceFile
object, read the get-source docs.
Cleaning Output
Synchronously (use with caution in browsers):
1stack = stack.clean ()
...or (asynchronously):
1stack = await stack.cleanAsync ()
It does the following:
- Reads sources (if available)
- Excludes locations marked with the
isThirdParty
flag (library calls) - Excludes locations marked with a
// @hide
comment (user defined exclusion) - Merges repeated lines (via the
.mergeRepeatedLines
)
You can customize its behavior by overriding the isClean (entry, index)
predicate.
Custom isThirdParty
Predicate
You can override the isThirdParty
behavior by subclassing StackTracey
:
1class MyStackTracey extends StackTracey { 2 3 isThirdParty (path, externalDomain) { // you can use externalDomain to include traces from libs from other domains 4 return (super.isThirdParty (path) // include default behavior 5 || path.includes ('my-lib')) // paths including 'my-lib' will be marked as thirdParty 6 && !path.includes ('jquery') // jquery paths won't be marked as thirdParty 7 } 8} 9 10... 11 12const stack = new MyStackTracey (error).withSources ()
Pretty Printing
1const prettyPrintedString = new StackTracey (error).withSources ().asTable ()
1const prettyPrintedString = (await new StackTracey (error).withSourcesAsync ()).asTable () // asynchronous version
...or (for pretty printing cleaned output):
1const prettyPrintedString = new StackTracey (error).clean ().asTable ()
1const prettyPrintedString = (await new StackTracey (error).cleanAsync ()).asTable () // asynchronous version
It produces a nice compact table layout (thanks to as-table
), supplied with source lines (if available):
at shouldBeVisibleInStackTrace test.js:25 const shouldBeVisibleInStackTrace = () => new StackTracey ()
at it test.js:100 const stack = shouldBeVisibleInStackTrace ()
at callFn mocha/lib/runnable.js:326 var result = fn.call(ctx);
at run mocha/lib/runnable.js:319 callFn(this.fn);
at runTest mocha/lib/runner.js:422 test.run(fn);
at mocha/lib/runner.js:528 self.runTest(function(err) {
at next mocha/lib/runner.js:342 return fn();
at mocha/lib/runner.js:352 next(suites.pop());
at next mocha/lib/runner.js:284 return fn();
at <anonymous> mocha/lib/runner.js:320 next(0);
If you find your pretty printed tables undesirably trimmed (or maybe too long to fit in the line), you can provide custom column widths when calling asTable
(...or, alternatively, by overriding maxColumnWidths ()
method):
1stack.asTable ({ 2 callee: 30, 3 file: 60, 4 sourceLine: 80 5})
Using As A Custom Exception Printer In Node
You can even replace the default NodeJS exception printer with this! This is how you can do it:
1process.on ('uncaughtException', e => { /* print the stack here */ }) 2process.on ('unhandledRejection', e => { /* print the stack here */ })
But the most simple way to achieve that is to use the ololog
library (that is built upon StackTracey and several other handy libraries coded by me). Check it out, it's pretty awesome and will blow your brains out :)
1const log = require ('ololog').handleNodeErrors () 2 3// you can also print Errors by simply passing them to the log() function
Parsing SyntaxError
instances
For example, when trying to require
a file named test_files/syntax_error.js
:
1// next line contains a syntax error (not a valid JavaScript) 2foo->bar ()
...the pretty printed call stack for the error thrown would be something like:
at (syntax error) test_files/syntax_error.js:2 foo->bar ()
at it test.js:184 try { require ('./test_files/syntax_error.js') }
at runCallback timers.js:781
at tryOnImmediate timers.js:743
at processImmediate [as _immediat timers.js:714
...where the first line is generated from parsing the raw output from the util.inspect
call in Node. Unfortunately, this won't work in older versions of Node (v4 and below) as these versions can't provide any meaningful information for a SyntaxError
instance.
Array Methods
All StackTracey instances expose map
, filter
, concat
and slice
methods. These methods will return mapped, filtered, joined, reversed and sliced StackTracey
instances, respectively:
1s = new StackTracey ().slice (1).filter (x => !x.thirdParty) // current stack shifted by 1 and cleaned from library calls 2 3s instanceof StackTracey // true
Extra Stuff
You can compare two locations via this predicate (tests file
, line
and column
for equality):
1StackTracey.locationsEqual (a, b)
To force-reload the sources, you can invalidate the global source cache:
1StackTracey.resetCache ()
Projects That Use StackTracey
- Ololog — a better
console.log
for the log-driven debugging junkies! - CCXT — a cryptocurrency trading library that supports 130+ exchanges
- pnpm — a fast, disk space efficient package manager (faster than npm and Yarn!)
- panic-overlay — a lightweight standalone alternative to
react-error-overlay
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: The Unlicense: LICENSE:0
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 2 are checked with a SAST tool
Reason
38 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-h6ch-v84p-w6p9
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-g6ww-v8xp-vmwg
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
1.7
/10
Last Scanned on 2025-01-20
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to stacktracey
@dcloudio/uni-stacktracey
@dcloudio/uni-stacktracey
@sc3d/stacktracey
Friendly fork of stacktracey to fix bugs.
@diahkomalasarinpm/dolores-repellendus-fugiat
[![Build Status](https://travis-ci.org/xpl/stacktracey.svg?branch=master)](https://travis-ci.org/xpl/stacktracey) [![Windows Build Status](https://ci.appveyor.com/api/projects/status/cvuygb8grrvm1sdm?svg=true)](https://ci.appveyor.com/project/xpl/stacktra