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

better-stack-traces

v1.1.0

Published

Detailed stack traces with code snippets.

Downloads

2,973

Readme

Better Stack Traces

Ever find yourself digging line-by-line through a Javascript stack trace to find the "real" error?

Error: ENOENT, no such file or directory '/bad/file/path'
    at Object.openSync (fs.js:230:18)
    at Object.readFileSync (fs.js:120:15)
    at readMyFile (/path/to/some/code/example.js:10:6)
    at doStuff (/path/to/some/code/example.js:5:10)
    at Object.<anonymous> (/path/to/some/code/example.js:13:1)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:31)

With better-stack-traces, you get inline code snippets instead:

Error: ENOENT, no such file or directory '/bad/file/path'
    at Object.openSync (fs.js:230:18)
    at Object.readFileSync (fs.js:120:15)
    at readMyFile (/path/to/some/code/example.js:10:6)
    ──────────────────────────────────────────────────
     8 » 
     9 » function readMyFile(filePath) {
    10 »   fs.readFileSync(filePath);
    •••••••••••
    11 » }
    12 » 
    13 » doStuff(readMyFile)
    
    at doStuff (/path/to/some/code/example.js:5:10)
    ───────────────────────────────────────────────
    3 » 
    4 » function doStuff(readCallback) {
    5 »   return readCallback("/bad/file/path");
    ••••••••••••••
    6 » }
    7 » 
    8 » 
    
    at Object.<anonymous> (/path/to/some/code/example.js:13:1)
    ──────────────────────────────────────────────────────────
    11 » }
    12 » 
    13 » doStuff(readMyFile)
    ••••••
    
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:31)

Usage

  1. Install the module using npm install better-stack-traces
  2. At the top of your code, require("better-stack-traces").register()
  • Alternatively, require("better-stack-traces/register"). This is useful for tools that have commandline options like --require in mocha.
  1. Enjoy easier-to-read stack traces

Using better-stack-traces with CoffeeScript

If you develop with CoffeeScript, better-stack-traces will automatically compile .coffee files when rendering the trace. Maybe someday we will have proper line number mapping, but until then better stack traces might help your sanity :)

Customizing the output

Stack traces not quite better enough? There are a few options to tweak the output:

require("better-stack-traces").register({
  before: 2, // number of lines to show above the error
  after: 3, // number of lines to show below the error
  maxColumns: 80, // maximum number of columns to output in code snippets
  collapseLibraries: /node_modules/ // omit code snippets from paths that match the given regexp (ignores node_modules by default)
})

If you want to get really fancy, take a look at the library itself. You can subclass BetterStackTrace and register your customized subclass instead.

Related resources

If you find better-stack-traces useful, you might be interested in these:

Contributors

  • Wilker Lúcio (wilkerlucio)
    • allow regex for collapseLibraries