# error-to-json

> Returns a JSON representation of an error (handles nested errors and calls nested toJSONs)

Latest version **2.0.1** (published 2025-05-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install error-to-json
pnpm add error-to-json
yarn add error-to-json
bun add error-to-json
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2025-05-30 |
| First published | 2016-05-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 26.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Tejesh Mehta |
| Maintainers | tjmehta |
| Keywords | error, tojson, to, json, return, nested, errortojson, err, errtojson |

## Links

- npm: https://www.npmjs.com/package/error-to-json
- Repository: https://github.com/tjmehta/error-to-json
- Issues: https://github.com/tjmehta/error-to-json/issues
- npm.io page: https://npm.io/package/error-to-json

## Dependencies (1)

- [fast-safe-stringify](https://npm.io/package/fast-safe-stringify.md) ^2.0.7

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.1 (latest) — 2025-05-30
- 2.0.0 — 2020-06-12
- 1.1.0 — 2016-07-04
- 1.0.1 — 2016-06-04
- 1.0.0 — 2016-05-04

## README

# error-to-json [![Build Status](https://travis-ci.org/tjmehta/error-to-json.svg?branch=master)](https://travis-ci.org/tjmehta/error-to-json)

Returns a JSON representation of an error (handles nested errors and calls nested toJSONs)

# Installation

```bash
npm i --save error-to-json
```

# Usage

#### Supports both ESM and CommonJS

```js
// esm
import errorToJSON from 'error-to-json'
// commonjs
const errorToJSON = require('error-to-json').default
```

#### Example: toJSON

```js
import errorToJSON from 'error-to-json'

var model = {
  _json: {
    x: 1,
    y: 2
  }
  helper: function () {/*...*/},
  toJSON: function () {
    return this._json
  }
}
var err = new Error('boom')
err.data = {
  err: new TypeError('boom2'),
  model: model
}
var json = errorToJSON(err)
/*
{
  "name": "Error",
  "message": "boom",
  "stack": "Error: boom\n    ....",
  "data": {
    "err": { // nested error was also converted to json
      "name": "TypeError",
      "message": "boom2",
      "stack": "TypeError: boom2\n    ...."
    },
    "model": { // toJSON was called!
      "x": 1,
      "y": 2
    }
  }
}
 */

```

#### Example: Parse

```js
import { parse } from 'error-to-json'

var err = parse({
  message: 'boom2',
  stack: 'Error: boom2\n  at ...\n  at...',
})
/*
  > err
  [ Error: boom2 ]
  > err.stack
  `Error: boom2
    at ...
    at ...`
*/

var err2 = parse({
  message: 'boom',
  // no stack
})
/*
  > err2
  [ Error: boom ]
  > err2.stack
  'Error: boom'
*/
```

#License
MIT

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