Gathering detailed insights and metrics for serialize-error-cjs
Gathering detailed insights and metrics for serialize-error-cjs
Gathering detailed insights and metrics for serialize-error-cjs
Gathering detailed insights and metrics for serialize-error-cjs
npm install serialize-error-cjs
Typescript
Module System
Node Version
NPM Version
99.8
Supply Chain
95.7
Quality
77.2
Maintenance
100
Vulnerability
100
License
TypeScript (86.87%)
JavaScript (13.13%)
Total Downloads
6,005,430
Last Day
6,369
Last Week
143,345
Last Month
607,644
Last Year
4,477,125
MIT-0 License
2 Stars
37 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Feb 09, 2025
Minified
Minified + Gzipped
Latest Version
0.2.0
Package Id
serialize-error-cjs@0.2.0
Unpacked Size
15.06 kB
Size
4.72 kB
File Count
9
NPM Version
10.9.2
Node Version
23.7.0
Published on
Feb 09, 2025
Cumulative downloads
Total Downloads
Last Day
7.2%
6,369
Compared to previous day
Last Week
-6.5%
143,345
Compared to previous week
Last Month
11.1%
607,644
Compared to previous month
Last Year
204.1%
4,477,125
Compared to previous year
Serialize/deserialize an error into a plain object in commonjs
Loosely based on serialize-error, but with support for commonjs projects.
Useful if you for example need to JSON.stringify()
or process.send()
the error.
1npm install serialize-error-cjs
1import {serializeError, deserializeError} from 'serialize-error-cjs'; 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-cjs
is a sub-dependency that's not used directly.
1import {errorConstructors} from 'serialize-error-cjs'; 2import {MyCustomError} from './errors.js' 3 4errorConstructors.set('MyCustomError', MyCustomError)
Warning: Only simple errors are supported. The constructor of errors classes will NOT be called during deserialization, so if your custom error relies on it being called it will not work.
Serialize an Error
object into a plain object.
Deserialize a plain object or any value into an Error
object.
No vulnerabilities found.
No security vulnerabilities found.