{"_id":"@cjser/serialize-error__v11_0_3","name":"@cjser/serialize-error__v11_0_3","dist-tags":{"latest":"11.0.3-cjser.2"},"versions":{"11.0.3-cjser.2":{"name":"@cjser/serialize-error__v11_0_3","version":"11.0.3-cjser.2","description":"Serialize/deserialize an error into a plain object","license":"MIT","repository":{"type":"git","url":"https://code.moenext.com/3rdeye/cjser.git"},"funding":"https://github.com/sponsors/sindresorhus","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":{"require":"./dist-cjser/index.cjs","default":"./index.js"},"sideEffects":false,"engines":{"node":">=14.16"},"scripts":{"//test":"xo && ava && tsd","test":"ava && tsd"},"keywords":["error","serialize","stringify","object","convert","process","send","cause","deserialize"],"dependencies":{"type-fest":"^2.12.2"},"devDependencies":{"ava":"^4.2.0","tsd":"^0.20.0","xo":"^0.48.0"},"main":"./dist-cjser/index.cjs","cjser":{"sourceVersion":"11.0.3","cjserVersion":2,"original":{"name":"serialize-error","version":"11.0.3","exports":"./index.js","repository":"sindresorhus/serialize-error","dependencies":{"type-fest":"^2.12.2"},"files":["index.js","index.d.ts","error-constructors.js","error-constructors.d.ts"],"scripts":{"//test":"xo && ava && tsd","test":"ava && tsd"}}},"_id":"@cjser/serialize-error__v11_0_3@11.0.3-cjser.2","gitHead":"a4bef54d7d22d3d9a6b84528457938d84cd65fe7","_nodeVersion":"20.14.0","_npmVersion":"10.7.0","dist":{"integrity":"sha512-4PATKaY4rCqI8X3jcfIOYA1gnQu9foKtS/ztHkeCU08RMhVdO+ieQmuaOgXAV5rBdl2Ee8SEJsl0tddGHHLvvg==","shasum":"04fb74a7c2f0d6550199677d754b8088d51234a7","tarball":"https://registry.npmjs.org/@cjser/serialize-error__v11_0_3/-/serialize-error__v11_0_3-11.0.3-cjser.2.tgz","fileCount":8,"unpackedSize":23869,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQDvg8bZ+lAushUAu6vtMHyDHHu29rNPJ8XjCmuZdB/yHwIgfoyqGP5EqQILXy1Z9rf+S5R19DQB10MHyhcCAleC2Bo="}]},"_npmUser":{"name":"nanahira","email":"nanahira@momobako.com"},"directories":{},"maintainers":[{"name":"nanahira","email":"nanahira@momobako.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/serialize-error__v11_0_3_11.0.3-cjser.2_1778159944191_0.03324434746309879"},"_hasShrinkwrap":false}},"time":{"created":"2026-05-07T13:19:04.103Z","11.0.3-cjser.2":"2026-05-07T13:19:04.338Z","modified":"2026-05-07T13:19:04.563Z"},"maintainers":[{"name":"nanahira","email":"nanahira@momobako.com"}],"description":"Serialize/deserialize an error into a plain object","keywords":["error","serialize","stringify","object","convert","process","send","cause","deserialize"],"repository":{"type":"git","url":"https://code.moenext.com/3rdeye/cjser.git"},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"license":"MIT","readme":"# serialize-error\n\n> Serialize/deserialize an error into a plain object\n\nUseful if you for example need to `JSON.stringify()` or `process.send()` the error.\n\n## Install\n\n```sh\nnpm install serialize-error\n```\n\n## Usage\n\n```js\nimport {serializeError, deserializeError} from 'serialize-error';\n\nconst error = new Error('🦄');\n\nconsole.log(error);\n//=> [Error: 🦄]\n\nconst serialized = serializeError(error);\n\nconsole.log(serialized);\n//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\\n    at Object.<anonymous> …'}\n\nconst deserialized = deserializeError(serialized);\n\nconsole.log(deserialized);\n//=> [Error: 🦄]\n```\n\n### Error constructors\n\nWhen 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:\n\n```js\nimport {deserializeError} from 'serialize-error';\n\nconst known = deserializeError({\n\tname: 'TypeError',\n\tmessage: '🦄'\n});\n\nconsole.log(known);\n//=> [TypeError: 🦄] <-- Still a TypeError\n\nconst unknown = deserializeError({\n\tname: 'TooManyCooksError',\n\tmessage: '🦄'\n});\n\nconsole.log(unknown);\n//=> [Error: 🦄] <-- Just a regular Error\n```\n\nThe [list of known errors](./error-constructors.js) can be extended globally. This also works if `serialize-error` is a sub-dependency that's not used directly.\n\n```js\nimport {errorConstructors} from 'serialize-error';\nimport {MyCustomError} from './errors.js'\n\nerrorConstructors.set('MyCustomError', MyCustomError)\n```\n\n**Warning:** Only simple and standard error constructors are supported, like `new MyCustomError(message)`. If your error constructor **requires** a second parameter or does not accept a string as first parameter, adding it to this map **will** break the deserialization.\n\n## API\n\n### serializeError(value, options?)\n\nSerialize an `Error` object into a plain object.\n\n- Non-error values are passed through.\n- Custom properties are preserved.\n- Non-enumerable properties are kept non-enumerable (name, message, stack).\n- Enumerable properties are kept enumerable (all properties besides the non-enumerable ones).\n- Buffer properties are replaced with `[object Buffer]`.\n- Circular references are handled.\n- If the input object has a `.toJSON()` method, then it's called instead of serializing the object's properties.\n- It's up to `.toJSON()` implementation to handle circular references and enumerability of the properties.\n\n### value\n\nType: `Error | unknown`\n\n### toJSON implementation examples\n\n```js\nimport {serializeError} from 'serialize-error';\n\nclass ErrorWithDate extends Error {\n\tconstructor() {\n\t\tsuper();\n\t\tthis.date = new Date();\n\t}\n}\n\nconst error = new ErrorWithDate();\n\nserializeError(error);\n// => {date: '1970-01-01T00:00:00.000Z', name, message, stack}\n```\n\n```js\nimport {serializeError} from 'serialize-error';\n\nconst error = new Error('Unicorn');\n\nerror.horn = {\n\ttoJSON() {\n\t\treturn 'x';\n\t}\n};\n\nserializeError(error);\n// => {horn: 'x', name, message, stack}\n```\n\n### deserializeError(value, options?)\n\nDeserialize a plain object or any value into an `Error` object.\n\n- `Error` objects are passed through.\n- Objects that have at least a `message` property are interpreted as errors.\n- All other values are wrapped in a `NonError` error.\n- Custom properties are preserved.\n- Non-enumerable properties are kept non-enumerable (name, message, stack, cause).\n- Enumerable properties are kept enumerable (all properties besides the non-enumerable ones).\n- Circular references are handled.\n- [Native error constructors](./error-constructors.js) are preserved (TypeError, DOMException, etc) and [more can be added.](#error-constructors)\n\n### value\n\nType: `{message: string} | unknown`\n\n### options\n\nType: `object`\n\n#### maxDepth\n\nType: `number`\\\nDefault: `Number.POSITIVE_INFINITY`\n\nThe maximum depth of properties to preserve when serializing/deserializing.\n\n```js\nimport {serializeError} from 'serialize-error';\n\nconst error = new Error('🦄');\nerror.one = {two: {three: {}}};\n\nconsole.log(serializeError(error, {maxDepth: 1}));\n//=> {name: 'Error', message: '🦄', one: {}}\n\nconsole.log(serializeError(error, {maxDepth: 2}));\n//=> {name: 'Error', message: '🦄', one: { two: {}}}\n```\n\n#### useToJSON\n\nType: `boolean`\\\nDefault: `true`\n\nIndicate 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()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior) but you prefer to not use it.\n\n### isErrorLike(value)\n\nPredicate 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.\n\n```js\nimport {isErrorLike} from 'serialize-error';\n\nconst error = new Error('🦄');\nerror.one = {two: {three: {}}};\n\nisErrorLike({\n\tname: 'DOMException',\n\tmessage: 'It happened',\n\tstack: 'at foo (index.js:2:9)',\n});\n//=> true\n\nisErrorLike(new Error('🦄'));\n//=> true\n\nisErrorLike(serializeError(new Error('🦄'));\n//=> true\n\nisErrorLike({\n\tname: 'Bluberricious pancakes',\n\tstack: 12,\n\tingredients: 'Blueberry',\n});\n//=> false\n```\n\n## cjser\n\nThis package is a CommonJS-compatible build generated by cjser for projects that still need `require()` support. The source version matches the original npm package version, with a cjser prerelease suffix for this generated build.\nOriginal repository: https://github.com/sindresorhus/serialize-error\n","readmeFilename":"readme.md","_rev":"1-d6d4bb71b2d9699b19b88b5267a98807"}