dir-compare is a Node.js library for comparing the contents and structure of two directories. It provides both synchronous (`compareSync`) and asynchronous (`compare`) comparison methods, supporting various strategies like size, content, and date comparison, along with advanced filtering options including glob patterns and `.gitignore` rules. The current stable version is 5.0.0, with regular updates addressing features, performance, and bug fixes. Key differentiators include its TypeScript support (since v4.0.0), significant performance improvements for large directory structures (e.g., 3x reduced heap usage and 2x faster content comparison since v4.0.0), and flexible extension points for custom comparators and result builders. The command-line interface (CLI) was moved to a separate package, `dir-compare-cli`, in v3.0.0.
npm install dir-compareVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates both synchronous and asynchronous directory comparison using `dir-compare`. It creates temporary directories, populates them with files, and then compares them using options for size and content, while excluding a specific file. It logs the comparison summary and detailed differences.
Carefully test existing comparison logic if `skipSubdirs` is used and adjust expectations or options if necessary.
If file name comparison is critical when comparing two specific files, implement a separate name check or ensure the files are placed within temporary directories for a full directory comparison.
For TypeScript projects, update imports to use named imports (e.g., `import { compare, Options } from 'dir-compare';`). JavaScript projects should continue to function but may benefit from type definitions.If you rely on the `dir-compare` CLI, install `dir-compare-cli` separately (`npm install -g dir-compare-cli`) and use its commands instead.
Review code that processes `diffSet` entries to leverage the new `origin` field for more precise handling of left-only vs. right-only differences, if needed.
Consider refactoring custom filtering logic to utilize the new `glob filter` and `.gitignore` implementation features provided by the library, which can improve maintainability and robustness.
For CommonJS, try `const { compare, compareSync } = require('dir-compare');` or for modern ES Modules: `import { compare, compareSync } from 'dir-compare';`. Ensure you are calling `compare` (async) with `.then()`/`await` or `compareSync` (sync) appropriately.Ensure the user running the Node.js application has read and execute permissions on all directories and files within the comparison paths. Alternatively, `dir-compare` added support for handling permission denied errors in v3.2.0; investigate options for graceful error handling within the comparison process if appropriate.
Wrap the comparison call in a `try...catch` block (for sync) or add a `.catch()` handler (for async) to properly handle potential errors during the comparison process. Also, verify that the input paths are valid and accessible.