Gathering detailed insights and metrics for serialize-error
Gathering detailed insights and metrics for serialize-error
Gathering detailed insights and metrics for serialize-error
Gathering detailed insights and metrics for serialize-error
serialize-error-cjs
> Serialize/deserialize an error into a plain object in commonjs
@types/serialize-error
Stub TypeScript definitions entry for serialize-error, which provides its own types definitions
@dotcom-reliability-kit/serialize-error
A utility function to serialize an error object in a way that's friendly to loggers, view engines, and converting to JSON
@tapjs/node-serialize
Stream TAP test data as a serialized node:test stream
Serialize/deserialize an error into a plain object
npm install serialize-error
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.7
Supply Chain
99.5
Quality
76.9
Maintenance
100
Vulnerability
99.6
License
JavaScript (96.79%)
TypeScript (3.21%)
Total Downloads
1,398,119,783
Last Day
478,125
Last Week
8,890,523
Last Month
38,986,392
Last Year
390,116,976
MIT License
558 Stars
84 Commits
65 Forks
9 Watchers
1 Branches
22 Contributors
Updated on May 28, 2025
Minified
Minified + Gzipped
Latest Version
12.0.0
Package Id
serialize-error@12.0.0
Unpacked Size
17.04 kB
Size
5.28 kB
File Count
7
NPM Version
10.9.0
Node Version
23.3.0
Published on
Jan 05, 2025
Cumulative downloads
Total Downloads
Last Day
-4.4%
478,125
Compared to previous day
Last Week
-7.9%
8,890,523
Compared to previous week
Last Month
3.7%
38,986,392
Compared to previous month
Last Year
19.8%
390,116,976
Compared to previous year
1
4
Serialize/deserialize an error into a plain object
Useful if you for example need to JSON.stringify()
or process.send()
the error.
1npm install serialize-error
1import {serializeError, deserializeError} from 'serialize-error'; 2 3const error = new Error('🦄'); 4 5console.log(error); 6//=> [Error: 🦄] 7 8const serialized = serializeError(error); 9 10console.log(serialized); 11//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'} 12 13const deserialized = deserializeError(serialized); 14 15console.log(deserialized); 16//=> [Error: 🦄]
When a serialized error with a known name
is encountered, it will be deserialized using the corresponding error constructor, while unknown error names will be deserialized as regular errors:
1import {deserializeError} from 'serialize-error'; 2 3const known = deserializeError({ 4 name: 'TypeError', 5 message: '🦄' 6}); 7 8console.log(known); 9//=> [TypeError: 🦄] <-- Still a TypeError 10 11const unknown = deserializeError({ 12 name: 'TooManyCooksError', 13 message: '🦄' 14}); 15 16console.log(unknown); 17//=> [Error: 🦄] <-- Just a regular Error
The list of known errors can be extended globally. This also works if serialize-error
is a sub-dependency that's not used directly.
1import {addKnownErrorConstructor} from 'serialize-error'; 2import {MyCustomError} from './errors.js' 3 4addKnownErrorConstructor(MyCustomError);
Warning: The constructor must work without any arguments or this function will throw.
Serialize an Error
object into a plain object.
[object Buffer]
..toJSON()
method, then it's called instead of serializing the object's properties..toJSON()
implementation to handle circular references and enumerability of the properties.Type: Error | unknown
1import {serializeError} from 'serialize-error'; 2 3class ErrorWithDate extends Error { 4 constructor() { 5 super(); 6 this.date = new Date(); 7 } 8} 9 10const error = new ErrorWithDate(); 11 12serializeError(error); 13// => {date: '1970-01-01T00:00:00.000Z', name, message, stack}
1import {serializeError} from 'serialize-error'; 2 3const error = new Error('Unicorn'); 4 5error.horn = { 6 toJSON() { 7 return 'x'; 8 } 9}; 10 11serializeError(error); 12// => {horn: 'x', name, message, stack}
Deserialize a plain object or any value into an Error
object.
Error
objects are passed through.message
property are interpreted as errors.NonError
error.Type: {message: string} | unknown
Type: object
Type: number
Default: Number.POSITIVE_INFINITY
The maximum depth of properties to preserve when serializing/deserializing.
1import {serializeError} from 'serialize-error'; 2 3const error = new Error('🦄'); 4error.one = {two: {three: {}}}; 5 6console.log(serializeError(error, {maxDepth: 1})); 7//=> {name: 'Error', message: '🦄', one: {}} 8 9console.log(serializeError(error, {maxDepth: 2})); 10//=> {name: 'Error', message: '🦄', one: { two: {}}}
Type: boolean
Default: true
Indicate whether to use a .toJSON()
method if encountered in the object. This is useful when a custom error implements its own serialization logic via .toJSON()
but you prefer to not use it.
Predicate to determine whether a value looks like an error, even if it's not an instance of Error
. It must have at least the name
, message
, and stack
properties.
1import {isErrorLike} from 'serialize-error'; 2 3const error = new Error('🦄'); 4error.one = {two: {three: {}}}; 5 6isErrorLike({ 7 name: 'DOMException', 8 message: 'It happened', 9 stack: 'at foo (index.js:2:9)', 10}); 11//=> true 12 13isErrorLike(new Error('🦄')); 14//=> true 15 16isErrorLike(serializeError(new Error('🦄')); 17//=> true 18 19isErrorLike({ 20 name: 'Bluberricious pancakes', 21 stack: 12, 22 ingredients: 'Blueberry', 23}); 24//=> false
No vulnerabilities found.
Reason
security policy file detected
Details
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 14/30 approved changesets -- score normalized to 4
Reason
0 commit(s) and 3 issue activity found in the last 90 days -- score normalized to 2
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-05-26
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More