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 🙏

© 2025 – Pkg Stats / Ryan Hefner

error-stringify

v0.9.1

Published

Augment errors with `Error#toJSON` that preserves `name`, `message`, `stack` as well as any other normally stringified attribute.

Downloads

249

Readme

error-stringify

Augment errors with Error#toJSON that preserves name, message, stack as well as any other normally stringified attribute.

Motive (use-cases)

  • As a Developer
  • I would like to preserve error attributes in JSON stringification
  • So that I can presever the props above while I:
    • output errors in loggers with log.warn("error %j", error)
    • dump errors to disk
    • stringify errors and send them over the network (to couchdb, mongo, log-stash, or any other service)

Features:

  • control which errors should include this behavior: all errors, or only my custom errors depend what target I pass
  • control wether the stack trace should be returned raw, or split by new line (the later is useful for improving readability of stack traces in Kibana and such)
  • control wether the stringification should include only properties from the stringified instance or all enumerable properties available via the prototype chain. (the later is useful when your custom errors inheritence tree defines names, categories/tags and codes on the prototype)
  • optimized for minimal memory footprint and high performance:
    • augments with specific slim optimized implementations that match your configs(instead of one with logic inside)
    • does not declare any symbols in any closure, except module.export

Usage:

The usage vary between the following two extremes.

Pass what you need to control the behavior :)

applying default behavior on the global Error

  • default target: global Error
  • default behavior:
    • stacks are not split
    • prototype chain is not included
require('error-stringify')()

Default behavior does not split stack traces, and includes only properties on the passed instance (enumerable or not).

applying behavior by configuration only to custom errors

  • provide target through the options.target parameter, or by the target
  • explicit switches
    • splitStackTrace - boolean
    • includeProtoChain - boolean
var AppError = require('./app-error-base');
require('error-stringify')({
    target            : AppError
    splitStackTrace   : true, 
    includeProtoChain : true
})

which is effectively the equivalent of:

var AppError = require('./app-error-base');
var options  = {
    splitStackTrace   : true, 
    includeProtoChain : true
};
require('error-stringify')(options, AppError)

The later form is useful when you want to pick the options from a file and pass it as is, so you can pass the base Error constructor in the 2nd argument.

allow override toJSON (side by side with Axios)

(see https://github.com/axios/axios/issues/6690)

require('error-stringify')({
    allowOverrideToJSON : true
})

Installation

npm install error-stringify --save

Alternatives I looked into:

  • (stringify-error)[https://www.npmjs.com/package/stringify-error]
  • (error-tojson)[https://www.npmjs.com/package/error-tojson]
  • (utils-error-to-json)[https://github.com/kgryte/utils-error-to-json]

Lisence

MIT, and that's it

Happy coding :)