serialize-to-js is a utility library for converting JavaScript objects into a string representation that can be safely evaluated as JavaScript code. Unlike `JSON.stringify`, it supports a wider range of JavaScript types including `RegExp`, `Date`, `Buffer`, `Set`, `Map`, `Error`, and various `TypedArray` types, while also handling circular references. The current stable version is 3.1.2. The library primarily focuses on robust serialization to executable JavaScript strings and has undergone breaking changes to enhance security, notably by removing the `deserialize` function in v2.0.0 due to Denial-of-Service vulnerabilities. It is particularly useful for scenarios requiring the exact re-creation of JavaScript objects, including their methods and non-primitive types, in environments where `eval` can be controlled.
npm install serialize-to-jsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to serialize a complex JavaScript object, including various primitive types, objects, arrays, regular expressions, dates, buffers, sets, and maps, into a JavaScript string. It shows the output format and hints at how to (cautiously) deserialize it.
Do not use `deserialize`. If you need to re-create objects from the serialized string, evaluate the string (e.g., using `eval()`) in a strictly controlled and trusted environment, or implement a custom, secure deserialization logic.
Only deserialize strings that originate from trusted sources. For untrusted input, use safer parsing methods like `JSON.parse` if your data can be represented in JSON, or implement strict validation and sandboxing around `eval()`.
Be aware that the `opts` object passed to `serialize` will be modified if `opts.reference` is true. If you need to preserve the original `opts` object, pass a shallow copy (e.g., `{ ...opts, reference: true }`).Remove all calls to `deserialize`. The package no longer provides a direct deserialization function. If you need to re-create the object, consider using `eval()` in a secure, controlled context with trusted input, or implement a custom parser.
If running in a browser, ensure you have a `Buffer` polyfill (e.g., `buffer` npm package) imported and made globally available, or avoid serializing `Buffer` objects in client-side code where `Buffer` is not native.
No dependency data recorded yet.