{"id":11134,"library":"javascript-stringify","title":"JavaScript Stringify","description":"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.","status":"active","version":"2.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/blakeembrey/javascript-stringify","tags":["javascript","stringify","object","eval","string","code","typescript"],"install":[{"cmd":"npm install javascript-stringify","lang":"bash","label":"npm"},{"cmd":"yarn add javascript-stringify","lang":"bash","label":"yarn"},{"cmd":"pnpm add javascript-stringify","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const stringify = require('javascript-stringify');","symbol":"stringify","correct":"import { stringify } from 'javascript-stringify';"},{"note":"TypeScript types are available since v2.0.0. Import `StringifyOptions` for type hints when configuring the `stringify` function.","symbol":"StringifyOptions","correct":"import type { StringifyOptions } from 'javascript-stringify';"}],"quickstart":{"code":"import { stringify } from 'javascript-stringify';\n\nconst exampleData = {\n  id: 1,\n  name: 'Complex Object',\n  date: new Date('2023-01-15T10:00:00Z'),\n  regex: /\\btest\\b/gi,\n  status: null,\n  computed: function(a, b) { return a + b; },\n  nested: {\n    value: 42,\n    metadata: new Map([['key1', 'val1'], ['key2', 'val2']])\n  }\n};\n\n// Add a circular reference to demonstrate handling\nexampleData.selfRef = exampleData;\n\nconsole.log('Default stringify (circular references removed):');\nconsole.log(stringify(exampleData));\n\nconsole.log('\\nStringify with restored references (uses IIFE):');\nconsole.log(stringify(exampleData, null, 2, { references: true }));\n\nconsole.log('\\nStringify with maxDepth:');\nconsole.log(stringify(exampleData, null, null, { maxDepth: 2 }));\n\nconst replacerExample = stringify(['foo', 123, 'bar'], function (value, indent, stringify) {\n  if (typeof value === 'string') {\n    return '\"' + value.toUpperCase() + '\"';\n  }\n  return stringify(value);\n});\nconsole.log('\\nStringify with custom replacer:');\nconsole.log(replacerExample);","lang":"typescript","description":"Demonstrates `stringify` with various data types, including functions, dates, regex, and options like `references` for circular handling and `maxDepth`."},"warnings":[{"fix":"Update your import statements: For ESM, use `import { stringify } from 'javascript-stringify';`. For CommonJS, use `const { stringify } = require('javascript-stringify');`.","message":"The 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.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Replace usages of `new Buffer()` with `Buffer.from()` or `Buffer.alloc()` in your application code, consistent with Node.js best practices.","message":"The `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.","severity":"breaking","affected_versions":">=2.1.0"},{"fix":"Pass `{ references: true }` in the options object to `stringify` if you intend for circular/repeated references to be restored as an IIFE.","message":"By 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.","severity":"gotcha","affected_versions":">=1.1.2"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":">=1.2.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you are using `import { stringify } from 'javascript-stringify';` for ESM or `const { stringify } = require('javascript-stringify');` for CommonJS.","cause":"Incorrect import statement after v2.0.0, attempting to use `require` or a default import when `stringify` is a named export.","error":"TypeError: (0 , javascript_stringify__WEBPACK_IMPORTED_MODULE_0__.stringify) is not a function"},{"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`.","cause":"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.","error":"TypeError: Converting circular structure to JSON"}],"ecosystem":"npm"}