Registry / data / diffparser

diffparser

JSON →
library2.0.1jsnpmunverified

diffparser is a robust JavaScript library specifically engineered for parsing unified diff format strings, which are the standard output for showing changes between files in version control systems like Git. Currently stable at version 2.0.1, the project appears to follow a feature-driven release cadence, with its latest major iteration focusing on modern JavaScript (ES2015) syntax and module patterns. A primary differentiator of diffparser is its capability to generate position information for individual changes, which is a critical requirement for integrating with services such as the GitHub Comments API for precise inline commenting. Furthermore, it offers an optional but powerful rename detection feature, allowing users to accurately identify files that have been renamed within a diff. This package serves as an actively maintained, ES2015-native spiritual successor to the earlier parse-diff project, providing a clean API and a comprehensive test suite for reliable diff processing in both Node.js and browser environments.

npm install diffparser
INSTALL
IMPORT
SIG · DIFFPARSER
D
diffparser
datajavascriptv2.0.1
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

parse
✓ import parse from 'diffparser';
✗ const parse = require('diffparser');
Since v2.0.0, diffparser is distributed as an ES module. CommonJS `require()` is not supported. Ensure your project is configured for ESM.

Demonstrates parsing a basic unified diff and an example with rename detection.

import parse from 'diffparser'; const diffContent = ` diff --git a/file b/file index 123..456 789 --- a/file +++ b/file @@ -1,2 +1,2 @@ - line1 + line2 `; const parsedDiff = parse(diffContent); console.log(JSON.stringify(parsedDiff, null, 2)); const diffWithRename = ` diff --git a/oldfile b/newfile rename from oldfile rename to newfile index 0000000..4e4b354 --- a/oldfile +++ b/newfile @@ -1 +1 @@ -hello +world `; const parsedRename = parse(diffWithRename, { findRenames: true }); console.log(JSON.stringify(parsedRename, null, 2));
Debug
Known issues
breakingdiffparser transitioned to an ES module (ESM) in version 2.0.0. This means traditional CommonJS `require()` syntax will no longer work, leading to import errors.
fix
Migrate your import statements from `const parse = require('diffparser');` to `import parse from 'diffparser';`. Ensure your Node.js environment or build setup supports ESM.
affects: >=2.0.0
gotchaThe parser expects input strings to strictly adhere to the unified diff format. Malformed or incomplete diffs can lead to parsing errors or unexpected output structures.
fix
Always validate or sanitize diff inputs to ensure they conform to the unified diff specification. Use robust error handling around the parse function.
affects: >=1.0.0
gotchaBy default, diffparser does not detect file renames. To identify renamed files, you must explicitly enable the `findRenames` option.
fix
When parsing diffs that might contain file renames, pass `{ findRenames: true }` as the second argument to the `parse` function: `parse(diffString, { findRenames: true })`.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use CommonJS `require()` syntax with diffparser v2.x or later, which is an ES module.
fix
Update your import statement from `const parse = require('diffparser');` to `import parse from 'diffparser';`.
TypeError: Cannot read properties of undefined (reading 'split') or similar parsing errors
The input string provided to `parse` is not a valid unified diff, or it's malformed/corrupted.
fix
Verify that the input string strictly adheres to the unified diff format. Check for missing headers, corrupted chunks, or unexpected line endings.
File renames are not detected in the parsed output.
The `findRenames` option was not enabled during parsing.
fix
Call the `parse` function with the `findRenames` option set to `true`: `parse(diffString, { findRenames: true });`
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
diffparser — npm install diffparser · libregistry