tosource is a utility library for JavaScript that serializes complex JavaScript objects, including functions, Date objects, RegExp instances, Maps, Sets, sparse arrays, NaN, Infinity, undefined, and negative zero, into their equivalent JavaScript source code string representations. This extends beyond the capabilities of JSON.stringify, which only handles a subset of these types. The package is currently in a pre-release alpha phase (v2.0.0-alpha.3), indicating active development. It is designed to facilitate sharing trusted data structures and even code between Node.js environments and browsers, useful for isomorphic applications. While there isn't an explicit release cadence, the recent alpha releases suggest ongoing improvements in functionality and toolchain, including TypeScript support and ESM exports.
npm install tosourceVerified import paths — ran on the pinned version, not inferred.
Demonstrates serialization of various JavaScript types, including functions, regular expressions, dates, Map, Set, and handling of circular references by marking them.
Upgrade your Node.js environment to version 10 or higher. For production, consider using a stable LTS version.
Review code that processes serialized output potentially containing negative zero to ensure compatibility with `Object.is` semantics.
Be aware that functions serialized by `tosource` are effectively re-created from source when evaluated. Do not rely on them to maintain runtime state or closures from the original context.
Understand that the deserialized object graph will not preserve referential equality for objects that were referenced multiple times in the original structure, unless they form a circular reference (which is handled differently).
Implement custom logic on the consuming end if you need to reconstruct circular references, or ensure your data structures do not contain them if exact reconstruction is critical.
Proceed with caution, monitor release notes for breaking changes, and thoroughly test in your specific environment before deploying to production. Consider locking to a specific alpha version.
For ESM, use `import toSource from 'tosource';`. For CommonJS, use `const toSource = require('tosource');`. Ensure `toSource` is within scope.Use the default import for ESM (`import toSource from 'tosource';`) or a standard `require` for CJS (`const toSource = require('tosource');`).Switch to `import toSource from 'tosource';` if your environment supports ESM. If you must use CommonJS, ensure your environment is configured for CJS modules.
Upgrade your Node.js runtime to version 10 or higher. You can use tools like `nvm` (Node Version Manager) to manage multiple Node.js versions.
No dependency data recorded yet.