Registry / serialization / javascript-stringify

javascript-stringify

JSON →
library2.1.0jsnpmunverified

The `javascript-stringify` library provides a function, `stringify`, that converts JavaScript values into a string representation that can be safely `eval`'d back into JavaScript, in contrast to `JSON.stringify` which outputs JSON. It supports serializing a wider range of JavaScript types, including regular expressions, `Date` objects, `Number` objects, `Error`, `Map`, `Set`, and most notably, functions (including ES methods, async, and generator functions). It also handles circular references by default omitting them or, with an option, restoring them via an IIFE. The current stable version is 2.1.0. Releases appear driven by bug fixes and feature enhancements rather than a strict cadence, with significant changes like TypeScript rewrite and ESM transition occurring in major versions. Its primary differentiator is the ability to produce evaluable JavaScript code, making it useful for scenarios like code generation or transmitting configuration that includes functional logic.

npm install javascript-stringify
INSTALL
IMPORT
SIG · JAVASCRIPT-STRINGI
J
javascript-stringify
serializationjavascriptv2.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

stringify
import { stringify } from 'javascript-stringify';
const stringify = require('javascript-stringify');
Since v2.0.0, the package primarily exports `stringify` as a named export, making the CommonJS `require` pattern for the default export incorrect. Use named imports for ESM environments.
StringifyOptions
import type { StringifyOptions } from 'javascript-stringify';
TypeScript types are available since v2.0.0. Import `StringifyOptions` for type hints when configuring the `stringify` function.

Demonstrates `stringify` with various data types, including functions, dates, regex, and options like `references` for circular handling and `maxDepth`.

import { stringify } from 'javascript-stringify'; const exampleData = { id: 1, name: 'Complex Object', date: new Date('2023-01-15T10:00:00Z'), regex: /\btest\b/gi, status: null, computed: function(a, b) { return a + b; }, nested: { value: 42, metadata: new Map([['key1', 'val1'], ['key2', 'val2']]) } }; // Add a circular reference to demonstrate handling exampleData.selfRef = exampleData; console.log('Default stringify (circular references removed):'); console.log(stringify(exampleData)); console.log('\nStringify with restored references (uses IIFE):'); console.log(stringify(exampleData, null, 2, { references: true })); console.log('\nStringify with maxDepth:'); console.log(stringify(exampleData, null, null, { maxDepth: 2 })); const replacerExample = stringify(['foo', 123, 'bar'], function (value, indent, stringify) { if (typeof value === 'string') { return '"' + value.toUpperCase() + '"'; } return stringify(value); }); console.log('\nStringify with custom replacer:'); console.log(replacerExample);
Debug
Known issues
breakingThe package was rewritten in TypeScript and changed its export mechanism. The primary `stringify` function is now a named export. Code relying on `require('javascript-stringify')` to get the function directly will break.
fix
Update your import statements: For ESM, use `import { stringify } from 'javascript-stringify';`. For CommonJS, use `const { stringify } = require('javascript-stringify');`.
affects: >=2.0.0
breakingThe `new Buffer()` constructor was deprecated in Node.js and has been removed from the package's internal implementation. While this might not directly break user code, it ensures compatibility with modern Node.js environments. If your own code still uses `new Buffer()`, it's a good time to update.
fix
Replace usages of `new Buffer()` with `Buffer.from()` or `Buffer.alloc()` in your application code, consistent with Node.js best practices.
affects: >=2.1.0
gotchaBy default, circular and repeated references in an object are removed during stringification to prevent infinite loops. This differs from `JSON.stringify` which would throw a `TypeError`. If you need to restore these references, you must enable the `references` option.
fix
Pass `{ references: true }` in the options object to `stringify` if you intend for circular/repeated references to be restored as an IIFE.
affects: >=1.1.2
gotchaThe `maxDepth` and `maxValues` options provide safeguards against stringifying extremely large or deeply nested objects, potentially truncating the output. Be aware of these defaults if your expected output is truncated.
fix
Adjust the `maxDepth` (default: 100) and `maxValues` (default: 100000) options in the `stringify` call if you are stringifying very large or deeply nested objects and require the full output.
affects: >=1.2.0
Errors
Common errors & fixes
TypeError: (0 , javascript_stringify__WEBPACK_IMPORTED_MODULE_0__.stringify) is not a function
Incorrect import statement after v2.0.0, attempting to use `require` or a default import when `stringify` is a named export.
fix
Ensure you are using `import { stringify } from 'javascript-stringify';` for ESM or `const { stringify } = require('javascript-stringify');` for CommonJS.
TypeError: Converting circular structure to JSON
This error typically occurs when using `JSON.stringify` with circular references. If you intended to use `javascript-stringify` but somehow invoked `JSON.stringify`, this would be the symptom.
fix
Ensure you are correctly calling `javascript-stringify`'s `stringify` function. If you need circular references restored, use the `references: true` option with `javascript-stringify`.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources