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 🙏

© 2026 – Pkg Stats / Ryan Hefner

sweet-error

v2.0.0

Published

Makes nodeJs Error cleaner, more readable and better formatted

Readme

SweetError

Makes nodeJs Error cleaner, more readable and better formatted

Installation

NodeJs

npm install sweet-error

Example usage

// cjs
const SweetError = require('sweet-error');
// mjs
import SweetError from 'sweet-error';

new SweetError(
  [
    'Invalid password',
    'A password should ... for example:',
    ['Abc242$3231@', '323CR#@#IY', 'sgfgsfg%$$5#22323'],
  ],
  {
    name: 'PwdValidator',
    code: 'PwdValidator.invalidPwd',
    exitCode: 1,
  }
);

Api reference

new SweetError(messages, [config])

Creates a new SweetError instance.

Parameters:

  • messages — An array of messages, where each message can be of any data type, and each index in the array represents a line.

    • Required
    • Type: any[]
  • config — Optional configuration object, see configuration properties

    • Optional
    • Type: Object

Configuration Properties

name

Sets the name of the current error instance.

This is typically used to identify the type of error when logging or displaying it in the console.

  • Optional
  • Type: string
  • Default: undefined

code

Sets the current instance error code.

This can be used to programmatically identify or handle specific error types.

  • Optional
  • Type: string
  • Default: undefined

Example :

new SweetError(['ACCESERR Error!'], {
  code: 'ACCESERR',
});

exitCode

Sets the code or signal name used to terminate the Node.js process when the instance triggers an exit.

  • Optional
  • Type: number | string
  • Default: undefined

autoExit

Controls whether the Node.js process should automatically exit after the error is logged.

Set this to true to terminate the process immediately after logging, or false to continue running and handle termination manually.

  • Optional
  • Type: boolean
  • Default: true

Example :

new SweetError({
  autoExit: false,
});

console.log('The process will NOT exit automatically');

colorize

Enables or disables text colorization in the console output.

When set to true, the error messages will include colors for better readability.
When false, all output will be plain text.

  • Optional
  • Type: boolean
  • Default: true

locationStyle

Determines how the error location (file, line, column) is formatted when displayed in the console.

This allows customization of the visual style of the location information for improved readability.

  • Optional
  • Type: LocationStyle
  • Default: 'label'

Example :

  • locationStyle: label — For readable, human-friendly format.

locationStyle: label

  • locationStyle: coords — Clickable links for IDEs / Standard IDE format.

locationStyle: coords

  • locationStyle: full — For most formal and complete format.

locationStyle: full

logger

A custom function to control how the SweetError output is rendered.
This function is provided via the configuration object when creating a new instance.

You can override this function to implement custom logging behavior, such as formatting output differently, or integrating with other logging systems.

  • Optional
  • Type: (this: SweetError) => void

Example:

new SweetError(['Custom output!'], {
  logger() {
    console.log(this.func.colorize('Custom error message'));
  },
});

Instance properties

.messages

An array of messages used to initialize the error instance.

  • Type: any[]

.name

The name of the error instance.

  • Type: string
  • Default: undefined

.code

The error code associated with this instance.

  • Type: string
  • Default: undefined

.exitCode

The code or signal name used to terminate the Node.js process.

  • Type: number | string
  • Default: undefined

.autoExit

Indicates whether the SweetError instance is configured to automatically exit the Node.js process after logging.

  • Type: boolean
  • Default: true

.locationStyle

Indicates how the error location (file, line, column) is formatted for this SweetError instance when rendered in the console.

  • Type: LocationStyle
  • Default: 'label'

.colorize

Indicates whether the SweetError instance will render messages with color in the console.

  • Type: boolean
  • Default: true

.stack

The stack trace captured by the SweetError instance at the time of creation.

  • Type: StackData[]
  • Readonly

.func

A read-only collection of string formatting and transformation utilities available on the SweetError instance.

These utilities are primarily intended for use inside a custom logger function to help customize the format and appearance of the error output.

  • Readonly

Utilities

  • colorize(sentence: string): string
    Applies random terminal colors to each word in a sentence using ANSI codes.

  • format(object: any): string
    Serializes an object into a JSON5-formatted string for pretty printing.

  • indent(string: string, padding: number): string
    Prepends a specific number of spaces to every line in a multiline string.

  • wrap(text: string, width: number): string
    Breaks a long string into multiple lines based on the specified character width.

Example :

new SweetError(['Error Occurred!'], {
  logger() {
    const { wrap } = this.func;

    console.log(wrap('This is a long message that will be wrapped', 40));
  },
});

License

This project is licensed under the MIT License — see the LICENSE file for details.

Changelog

See the CHANGELOG for a detailed history of all changes, versions, and releases.