npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nn-node-stacktrace

v0.2.0

Published

Node stack trace callsites and some other tiny utils to play with

Downloads

17

Readme

Node stack trace utils

Node stack trace callsites and some other tiny utils to play with.

NPM version NPM monthly download

  • Caution: This package uses some private APIs from Node, those APIs are not guaranteed to be kept overtime. It's also a new package under heavy developing and has not been well tested yet. If any problem happens, feel free to open an issue.
  • Require:
    • Node >= v8.0.0

Reference

Example

Get a CallSite object

const stacktrace = require('nn-node-stacktrace')

try {
  throw new Error('boooo')
  // or put some of your code instead
} catch(err) {
  const csArr = stacktrace.callsites(err)
  // see https://github.com/v8/v8/wiki/Stack-Trace-API
  console.log(csArr[0].getFileName(), csArr[0].getLineNumber())
}

Clean stack output string

Imagine that we have catched an error, and we got the default stack output like this mess:

TypeError: model.toSomething is not a function
    at Object.exports.login (/Users/user/nnm/awesome-project/src/handlers/login.js:22:15)
    at main (/Users/user/nnm/awesome-project/src/server.js:37:20)
    at Object.<anonymous> (/Users/user/nnm/awesome-project/src/server.js:35:1)
    at Module._compile (module.js:571:32)
    at loader (/Users/user/nnm/awesome-project/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/user/nnm/awesome-project/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)

Now let's say that we only have interesting with our src folder. And here is the cleaned result from stacktrace.clean(err):

    at login        handlers/login.js:22
    at main         server.js:37
    at <anonnymous> server.js:50

API

stacktrace.setCwd(cwd)

  • Set the current working directory
  • Parameters:
    • cwd:
      • string - Required

stacktrace.callsites(err)

  • Get the structured stack trace from an error using V8 Stack Trace API. See the Customizing stack traces section in V8 Stack Trace API for more detail.
  • Parameters:
    • err:
      • Error - Required
      • An Error object to get the structured stack trace from.
  • Return:
    • An array of CallSite objects, which holds the raw information for all stack layers.

stacktrace.clean(err[, filterFn, filenameFn])

  • Get the cleaned stack trace output string from an error.
  • Parameters:
    • err:
      • Error - Required
      • An Error object to get the cleaned stack trace output string.
    • filterFn:
      • Function - Optional - Default view source
      • A function to filter the stack layer.
    • filenameFn:
      • Function - Optional - Default view source
      • A function to get the shortened file name from the stack layer.
  • Return:
    • A cleaned stack trace string output. An emptry string is returned if there's no layer matches.

stacktrace.location([n, filterFn, filenameFn])

  • Get the location using file name and line number at the nth call layer.
  • Parameters:
    • n:
      • number - Optional - Default 0
      • The index of call layer which we want to trace.
    • filterFn:
      • Function - Optional - Default view source
      • A function to filter the stack layer.
    • filenameFn:
      • Function - Optional - Default view source
      • A function to get the shortened file name from the stack layer.
  • Return:
    • A string concatenated from the file name and line number. An emptry string is returned if there's no layer at the nth index.

License

MIT