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.
npm install diffVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic line-by-line text comparison and logs the changes with indicators.
Remove `@types/diff` dependency. Review `diffWords` usage for `ignoreWhitespace` and consider `diffWordsWithSpace`. Update code to reflect changes in `diffJson`'s `stringifyReplacer` and async function return types. Consult release notes for a full list of changes.
Review `diffWords` usage, particularly with `ignoreWhitespace`. Test extensively after upgrading from versions prior to v6. Consult the v6.0.0 release notes for specific API changes.
If using the UMD build, adjust global references from `JsDiff` to `Diff`. Be aware of changes in word diff tokenization and unified patch formatting.
Immediately upgrade to version 8.0.3 or later. If unable to upgrade, implement input validation to prevent parsing patches that contain `\r`, `\u2028`, or `\u2029` in any user-controlled patch content, especially headers.
Avoid direct imports from the `lib` folder. Always import directly from the top-level `diff` package (`import { diffWords } from 'diff';`) to ensure compatibility and correct module resolution. This was largely addressed with the v8 refactor.The `ignoreWhitespace` option for `diffWords` is deprecated in TypeScript since v8. Instead, use `diffWordsWithSpace` for word diffs where whitespace significance matters, or simply `diffWords` if whitespace should be ignored by default.
For CommonJS, use `const { parsePatch } = require('diff');`. For ESM, use `import { parsePatch } from 'diff';` or `import * as Diff from 'diff';` and then `Diff.parsePatch(...)`. Ensure your module system is configured correctly for ESM if using Node.js.Upgrade `diff` to version 8.0.3 or higher immediately. If unable to upgrade, sanitize patch input to remove `\r`, `\u2028`, or `\u2029` characters from filename and patch headers before processing.
Ensure the `dist/diff.js` or `dist/diff.min.js` file is correctly loaded via a `<script>` tag. The library exposes its API via a global `Diff` object in this context. Example: `<script src="node_modules/diff/dist/diff.min.js"></script>` and then `Diff.diffChars(...)`.
No dependency data recorded yet.