{"id":13070,"library":"diffparser","title":"Unified Diff Parser","description":"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.","status":"active","version":"2.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/fgnass/diffparser","tags":["javascript"],"install":[{"cmd":"npm install diffparser","lang":"bash","label":"npm"},{"cmd":"yarn add diffparser","lang":"bash","label":"yarn"},{"cmd":"pnpm add diffparser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v2.0.0, diffparser is distributed as an ES module. CommonJS `require()` is not supported. Ensure your project is configured for ESM.","wrong":"const parse = require('diffparser');","symbol":"parse","correct":"import parse from 'diffparser';"}],"quickstart":{"code":"import parse from 'diffparser';\n\nconst diffContent = `\ndiff --git a/file b/file\nindex 123..456 789\n--- a/file\n+++ b/file\n@@ -1,2 +1,2 @@\n- line1\n+ line2\n`;\n\nconst parsedDiff = parse(diffContent);\nconsole.log(JSON.stringify(parsedDiff, null, 2));\n\nconst diffWithRename = `\ndiff --git a/oldfile b/newfile\nrename from oldfile\nrename to newfile\nindex 0000000..4e4b354\n--- a/oldfile\n+++ b/newfile\n@@ -1 +1 @@\n-hello\n+world\n`;\n\nconst parsedRename = parse(diffWithRename, { findRenames: true });\nconsole.log(JSON.stringify(parsedRename, null, 2));","lang":"javascript","description":"Demonstrates parsing a basic unified diff and an example with rename detection."},"warnings":[{"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.","message":"diffparser 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.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Always validate or sanitize diff inputs to ensure they conform to the unified diff specification. Use robust error handling around the parse function.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"When parsing diffs that might contain file renames, pass `{ findRenames: true }` as the second argument to the `parse` function: `parse(diffString, { findRenames: true })`.","message":"By default, diffparser does not detect file renames. To identify renamed files, you must explicitly enable the `findRenames` option.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Update your import statement from `const parse = require('diffparser');` to `import parse from 'diffparser';`.","cause":"Attempting to use CommonJS `require()` syntax with diffparser v2.x or later, which is an ES module.","error":"ReferenceError: require is not defined"},{"fix":"Verify that the input string strictly adheres to the unified diff format. Check for missing headers, corrupted chunks, or unexpected line endings.","cause":"The input string provided to `parse` is not a valid unified diff, or it's malformed/corrupted.","error":"TypeError: Cannot read properties of undefined (reading 'split') or similar parsing errors"},{"fix":"Call the `parse` function with the `findRenames` option set to `true`: `parse(diffString, { findRenames: true });`","cause":"The `findRenames` option was not enabled during parsing.","error":"File renames are not detected in the parsed output."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}