Registry / data / diff

diff

JSON →
library9.0.0jsnpmunverified

The `diff` (also known as `jsdiff`) library provides a robust JavaScript implementation for calculating differences between texts. Based on the efficient Myers O(ND) algorithm, it offers various comparison granularities, including character, word, line, CSS token, and JSON object differencing. The library is currently at stable version 9.0.0, released recently (April 2026), and maintains an active development cycle with frequent updates addressing performance, features, and critical bug fixes. Major versions, particularly v6 and v8, have introduced significant breaking changes and refactors, including a transition to bundled TypeScript types in v8. `jsdiff` stands out for its comprehensive API, enabling not only difference calculation but also the creation and application of unified diff patches. It supports modern Node.js environments and widely available browser runtimes, and is a zero-dependency package, making it a lightweight yet powerful utility for tasks like code reviews, version control, and data synchronization.

dataserialization
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.

ESM imports are preferred since v8. Named exports for all diffing functions.

import { diffLines } from 'diff';

ESM is the standard. CommonJS `require` can also be used but is less idiomatic for modern projects.

import { createPatch } from 'diff';

The library exports many named functions, not a default export. Use `import * as Diff from 'diff'` or destructure specific functions.

import * as Diff from 'diff';

Demonstrates a basic line-by-line text comparison and logs the changes with indicators.

import { diffLines } from 'diff'; const oldText = `Line One Line Two Line Three Line Four`; const newText = `Line A Line Two Changed Line Three Line B`; const changes = diffLines(oldText, newText); console.log('--- Diff Results ---'); changes.forEach((part) => { const color = part.added ? 'green' : part.removed ? 'red' : 'grey'; // For a real-world scenario, use a color library or styling for output const prefix = part.added ? '+ ' : part.removed ? '- ' : ' '; console.log(`${prefix}${part.value.replace(/\n/g, '\n' + prefix)}`, `(${color})`); }); /* Example Output: --- Diff Results --- + Line A (green) - Line One (red) Line Two (grey) + Changed (green) - Line Two (red) Line Three (grey) + Line B (green) - Line Four (red) */
Debug
Known footguns
breakingVersion 8.0.0 introduced significant breaking changes. The source code was rewritten in TypeScript, and the library now ships its own type definitions. Users previously relying on `@types/diff` must remove this dependency when upgrading to v8 or newer. Many exported types for options arguments were also removed or refactored. `diffWords` behavior changed, and `ignoreWhitespace: true` for `diffWords` is deprecated in TypeScript in favor of `diffWordsWithSpace`.
breakingVersion 6.0.0 contained numerous breaking changes, including a radical alteration to `diffWords` behavior where runs of whitespace were previously treated as tokens, leading to unintuitive diffing. This was part of a larger effort to fix open bugs and design problems requiring breaking changes.
breakingVersion 5.0.0 renamed the UMD export from `JsDiff` to `Diff`. Additionally, newlines were separated into distinct tokens for word diffs, and unified diffs were adjusted to match certain 'quirks' for broader compatibility.
breakingThe `parsePatch` and `applyPatch` methods in versions prior to 8.0.3, 5.2.2, 4.0.4, and 3.5.1 are vulnerable to Denial of Service (DoS) via infinite loops and inefficient regular expression complexity (ReDOS). Maliciously crafted patches containing specific line break characters (`\r`, `\u2028`, `\u2029`) in filename or patch headers can cause uncontrolled memory consumption or high CPU usage, leading to application crashes.
gotchaImporting specific internal modules directly, e.g., `require("diff/lib/diff/word.js")`, was broken in Node.js 17.5.0 and later due to changes in Node.js's interpretation of the `exports` field in `package.json`.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
16 hits · last 30 days
gptbot
4
ahrefsbot
4
bytedance
4
amazonbot
3
script
1
Resources