# pretty-exceptions

> Pretty and more helpful uncaught exceptions, automatically.

Latest version **2.0.2** (published 2023-09-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install pretty-exceptions
pnpm add pretty-exceptions
yarn add pretty-exceptions
bun add pretty-exceptions
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2023-09-05 |
| First published | 2017-04-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=14 |
| Dependencies | 3 |
| Unpacked size | 14.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 27 |
| Author | Ahmad Nassri |
| Maintainers | ahmadnassri |
| Keywords | pretty, exception, format, error |

## Links

- npm: https://www.npmjs.com/package/pretty-exceptions
- Repository: https://github.com/ahmadnassri/node-pretty-exceptions
- Issues: https://github.com/ahmadnassri/node-pretty-exceptions/issues
- Funding: https://github.com/sponsors/ahmadnassri
- npm.io page: https://npm.io/package/pretty-exceptions

## Dependencies (3)

- [chalk](https://npm.io/package/chalk.md) ^4.1.2
- [strip-ansi](https://npm.io/package/strip-ansi.md) ^6.0.1
- [error-stack-parser](https://npm.io/package/error-stack-parser.md) ^2.1.4

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 2.0.2 (latest) — 2023-09-05
- 2.0.1 — 2023-01-02
- 2.0.0 — 2022-07-28
- 1.4.15 — 2021-10-12
- 1.4.14 — 2021-10-12
- 1.4.13 — 2021-08-12
- 1.4.12 — 2021-07-30
- 1.4.11 — 2021-06-12
- 1.4.10 — 2021-05-11
- 1.4.9 — 2021-05-11
- 1.4.8 — 2021-04-21
- 1.4.7 — 2021-02-27
- 1.4.6 — 2021-02-02
- 1.4.5 — 2020-10-28
- 1.4.4 — 2020-10-28
- … 14 more at https://npm.io/package/pretty-exceptions/versions

## README

# Pretty Exceptions

Pretty and more helpful uncaught exceptions, automatically.

[![license][license-img]][license-url]
[![release][release-img]][release-url]
[![semantic][semantic-img]][semantic-url]

![][1]

## Usage

### Library

``` js
const pretty = require('pretty-exceptions/lib')

const options = {
  source: true,
  native: true,
  color: true,
  cwd: process.cwd()
}

const output = pretty(new Error('foo'), options)
```

#### `options`

| name       | type      | required | default         | description                              |
|------------|-----------|----------|-----------------|------------------------------------------|
| **source** | `Boolean` | ✖        | `false`         | Show source code in stack                |
| **native** | `Boolean` | ✖        | `false`         | Show native code in stack                |
| **color**  | `Boolean` | ✖        | `true`          | use ansi colors                          |
| **cwd**    | `String`  | ✖        | `process.cwd()` | resolve file names relative to this path |

### CLI *(preferred)*

This is the preferred approach to best avoid mistakingly including into production builds

1.  setup your [`NODE_PATH`][] environment variable

    ``` shell
    export NODE_PATH="$NODE_PATH:$(npm root -g)"
    ```

2.  install `pretty-exceptions`

    ``` shell
    npm install --global pretty-exceptions
    ```

3.  run your application with the [`--require`][] flag:

    ``` shell
    node --require pretty-exceptions my-app.js

    # alternative modes (see below):
    node --require pretty-exceptions/source my-app.js
    ```

#### Pro Tip:

Use Bash Aliases for less typing

``` shell
# make a special alias for debugging
alias node-ex='node --require pretty-exceptions'

# or even better:
alias node='node --require pretty-exceptions/source'
```

> To make this persistent, you need to add this into your `~/.bashrc` *(or `~/.bash_profile`)*.

#### Environment Variables

| env                        | default | description               |
|----------------------------|---------|---------------------------|
| `PRETTY_EXCEPTIONS_SOURCE` | `0`     | Show source code in stack |
| `PRETTY_EXCEPTIONS_NATIVE` | `0`     | Show native code in stack |
| `PRETTY_EXCEPTIONS_COLOR`  | `1`     | use ansi colors           |

### Source *(if you really must!)*

Require at the top-most entry point of your app:

``` js
require('pretty-exceptions')

// alternative modes (see below):
require('pretty-exceptions/source-native')
```

## Modes

### Default

``` shell
$ node --require pretty-exceptions my-app.js
```

###### Output

``` plain
Error: oh no! this is an error message!
 │
 └┬╼ /path/to/my-app.js
  │
  ├──╼ someOtherFunction @ line 2:27
  ├──╼ someFunction @ line 6:3
  └──╼ Object.<anonymous> @ line 9:1
```

###### Demo

> Colors will vary based on your local terminal configuration

![][2]

### View Source

``` shell
$ node --require pretty-exceptions/source my-app.js
# OR
$ PRETTY_EXCEPTIONS_SOURCE=1 node --require pretty-exceptions my-app.js
```

###### Output

``` plain
Error: oh no! this is an error message!
 │
 └┬╼ /path/to/my-app.js
  │
  ├──╼ someOtherFunction @ line 2
  │
  │    function someOtherFunction () {
  ├╌╌╌╌╌╌╌╌╌╌╮
  │    throw new Error('oh no! this is an error message!')
  │    }
  │
  ├──╼ someFunction @ line 6
  │
  │    function someFunction () {
  ├╌╌╌╌╮
  │    someOtherFunction()
  │    }
  │
  ├──╼ Object.<anonymous> @ line 9
  │
  └╌╌╌╌╮
       someFunction()
```

###### Demo

> Colors will vary based on your local terminal configuration

![][3]

### View Native Calls

``` shell
$ node --require pretty-exceptions/native my-app.js
# OR
$ PRETTY_EXCEPTIONS_NATIVE=true node --require pretty-exceptions my-app.js
```

###### Output

``` plain
Error: oh no! this is an error message!
 │
 ├─┬╼ /path/to/my-app.js
 │ │
 │ ├──╼ someOtherFunction @ line 2:27
 │ ├──╼ someFunction @ line 6:3
 │ └──╼ Object.<anonymous> @ line 9:1
 │
 ├─┬╼ module.js
 │ │
 │ ├──╼ Module._compile @ line 571:32
 │ ├──╼ Object.Module._extensions..js @ line 580:10
 │ ├──╼ Module.load @ line 488:32
 │ ├──╼ tryModuleLoad @ line 447:12
 │ ├──╼ Function.Module._load @ line 439:3
 │ └──╼ Module.runMain @ line 605:10
 │
 └┬╼ bootstrap_node.js
  │
  └──╼ run @ line 423:7
```

###### Demo

> Colors will vary based on your local terminal configuration

![][4]

### View Source & Native

``` shell
$ node --require pretty-exceptions/source-native my-app.js
# OR
$ PRETTY_EXCEPTIONS_SOURCE=1 PRETTY_EXCEPTIONS_NATIVE=1 node --require pretty-exceptions my-app.js
```

###### Output

``` plain
Error: oh no! this is an error message!
 │
 ├─┬╼ /path/to/my-app.js
 │ │
 │ ├──╼ someOtherFunction @ line 2
 │ │
 │ │    function someOtherFunction () {
 │ ├╌╌╌╌╌╌╌╌╌╌╮
 │ │    throw new Error('oh no! this is an error message!')
 │ │    }
 │ │
 │ ├──╼ someFunction @ line 6
 │ │
 │ │    function someFunction () {
 │ ├╌╌╌╌╮
 │ │    someOtherFunction()
 │ │    }
 │ │
 │ ├──╼ Object.<anonymous> @ line 9
 │ │
 │ └╌╌╌╌╮
 │      someFunction()
 │ 
 │
 ├─┬╼ module.js
 │ │
 │ ├──╼ Module._compile @ line 571
 │ ├──╼ Object.Module._extensions..js @ line 580
 │ ├──╼ Module.load @ line 488
 │ ├──╼ tryModuleLoad @ line 447
 │ ├──╼ Function.Module._load @ line 439
 │ └──╼ Module.runMain @ line 605
 │
 └┬╼ bootstrap_node.js
  │
  └──╼ run @ line 423
```

###### Demo

> Colors will vary based on your local terminal configuration

![][5]

  [1]: ./screenshots/example.png
  [`NODE_PATH`]: https://nodejs.org/docs/latest/api/modules.html#modules_loading_from_the_global_folders
  [`--require`]: https://nodejs.org/api/cli.html#cli_r_require_module
  [2]: ./screenshots/default.png
  [3]: ./screenshots/source.png
  [4]: ./screenshots/native.png
  [5]: ./screenshots/source-native.png

----
> Author: [Ahmad Nassri](https://www.ahmadnassri.com/) &bull;
> Twitter: [@AhmadNassri](https://twitter.com/AhmadNassri)

[license-url]: LICENSE
[license-img]: https://badgen.net/github/license/ahmadnassri/node-pretty-exceptions

[release-url]: https://github.com/ahmadnassri/node-pretty-exceptions/releases
[release-img]: https://badgen.net/github/release/ahmadnassri/node-pretty-exceptions

[semantic-url]: https://github.com/ahmadnassri/node-pretty-exceptions/actions?query=workflow%3Arelease
[semantic-img]: https://badgen.net/badge/📦/semantically%20released/blue

---
_Source: https://npm.io/package/pretty-exceptions · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
