{"id":11212,"library":"klona","title":"klona - Deep Cloning Utility","description":"klona is a minimalist, high-performance utility for deep cloning JavaScript objects, arrays, dates, regexps, and other complex data types. Currently stable at version 2.0.6, it maintains a regular release cadence with frequent patches and minor improvements, as seen with multiple patch releases since v2.0.0. Its primary differentiator lies in its tiny footprint (ranging from 240B for `klona/json` to 501B for `klona/full` gzipped) and superior speed compared to many alternatives. It offers multiple \"modes\" (`klona/json`, `klona/lite`, `klona`, `klona/full`), allowing developers to import only the necessary functionality for specific data type support, thereby optimizing bundle size. It supports ESM, CommonJS, and UMD, and ships with TypeScript types.","status":"active","version":"2.0.6","language":"javascript","source_language":"en","source_url":"https://github.com/lukeed/klona","tags":["javascript","clone","copy","deep","extend","recursive","object","typescript"],"install":[{"cmd":"npm install klona","lang":"bash","label":"npm"},{"cmd":"yarn add klona","lang":"bash","label":"yarn"},{"cmd":"pnpm add klona","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v2.0.0, `klona` uses named exports for ESM. For CommonJS, use `const { klona } = require('klona');`","wrong":"import klona from 'klona';","symbol":"klona","correct":"import { klona } from 'klona';"},{"note":"The `klona/json` entry point provides a smaller bundle for cloning only JSON-serializable types. Uses named exports since v2.0.0.","wrong":"import klona from 'klona/json';","symbol":"klona (JSON mode)","correct":"import { klona } from 'klona/json';"},{"note":"The `klona/full` entry point provides the most comprehensive cloning, including Symbol and non-enumerable properties. Uses named exports since v2.0.0.","wrong":"import klona from 'klona/full';","symbol":"klona (Full mode)","correct":"import { klona } from 'klona/full';"},{"note":"Since v2.0.0, `klona` uses named exports for CommonJS.","wrong":"const klona = require('klona');","symbol":"klona (CommonJS)","correct":"const { klona } = require('klona');"}],"quickstart":{"code":"import { klona } from 'klona';\n\nconst input = {\n  foo: 1,\n  bar: {\n    baz: 2,\n    bat: {\n      hello: 'world'\n    }\n  },\n  date: new Date(),\n  regex: /test/g,\n  items: new Set([1, 2, 3]),\n  mapData: new Map([['a', 1], ['b', 2]])\n};\n\nconst output = klona(input);\n\n// Verify deep equality (requires an assertion library like Node's assert.deepStrictEqual)\n// import * as assert from 'assert';\n// assert.deepStrictEqual(input, output);\n\n// Modifying the clone does not affect the original\noutput.bar.bat.hola = 'mundo';\noutput.bar.baz = 99;\noutput.items.add(4);\noutput.date.setFullYear(2030);\n\nconsole.log('Original Input:', JSON.stringify(input, null, 2));\nconsole.log('Cloned Output (modified):', JSON.stringify(output, null, 2));\n\n// You would see 'hola' and 'mundo' only in the output, and 'baz' will be 99.\n// The date and set will also reflect the changes only in the output.\n","lang":"typescript","description":"Demonstrates how to import and use the `klona` function to perform a deep clone of a complex object, including nested objects, dates, regexps, sets, and maps, and verifies that modifications to the clone do not affect the original source object."},"warnings":[{"fix":"Update your imports: For ESM, change `import klona from 'klona'` to `import { klona } from 'klona'`. For CommonJS, change `const klona = require('klona')` to `const { klona } = require('klona')`. This applies to all `klona` modes (e.g., `klona/json`, `klona/lite`).","message":"Starting with v2.0.0, `klona` migrated from a default export to a named export. This affects both ESM `import` statements and CommonJS `require` calls.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Upgrade to `klona@2.0.5` or newer to ensure correct handling of `Object.assign` in class constructors.","message":"Prior to v2.0.5, `klona` and `klona/lite` had an issue where `Object.assign` within class constructors was not handled correctly during cloning, potentially leading to incorrect copies for custom class instances.","severity":"gotcha","affected_versions":"<2.0.5"},{"fix":"Upgrade to `klona@2.0.4` or newer to ensure that prototype methods are correctly copied when using the `klona/full` module.","message":"In `klona/full`, prototype methods were not consistently copied prior to v2.0.4, which could lead to incomplete deep clones for objects with prototype chains.","severity":"gotcha","affected_versions":"<2.0.4"},{"fix":"Upgrade to `klona@1.1.1` or newer to resolve issues with `Set` and `Map` cloning and benefit from improved prototype pollution protection.","message":"Versions prior to v1.1.1 had issues correctly handling `Set` and `Map` instances during cloning, and lacked explicit protection against `__proto__` pollution attacks.","severity":"gotcha","affected_versions":"<1.1.1"},{"fix":"Upgrade to `klona@2.0.6` or newer to gain proper TypeScript `nodenext` support.","message":"TypeScript users leveraging `moduleResolution: 'nodenext'` in their `tsconfig.json` might encounter type resolution issues with `klona` in versions prior to v2.0.6 due to missing 'types' condition in the package's `exports` map.","severity":"gotcha","affected_versions":"<2.0.6"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"For ESM, change `import klona from 'klona'` to `import { klona } from 'klona'`. For CommonJS, change `const klona = require('klona')` to `const { klona } = require('klona')`.","cause":"Attempting to use `klona` as a default import/require after the v2.0.0 breaking change to named exports.","error":"TypeError: klona is not a function"},{"fix":"Ensure your TypeScript version and configuration (`moduleResolution`) are compatible. Clear your editor's TypeScript cache if using VSCode. The correct import for v2+ is `import { klona } from 'klona'`.","cause":"Using an older TypeScript version or configuration with a `klona` version >=2.0.0, where the type definitions might be misinterpreted, or the editor cache is stale.","error":"Type error: Module ''klona'' has no exported member 'klona'. Did you mean to use 'import klona from 'klona'' instead?"}],"ecosystem":"npm"}