{"id":10735,"library":"detect-conflict","title":"Detect Conflict","description":"detect-conflict is a focused utility library designed to determine if new content for a file can be safely merged with an existing file on disk without introducing conflicts. It achieves this by performing a content-based comparison, returning `true` if a conflict is detected and `false` otherwise. The current stable version is 1.0.1. As a specialized utility, its release cadence is generally slow, with updates primarily addressing bug fixes or minor enhancements rather than frequent new features. It differentiates itself by providing a simple, direct API for conflict *detection* rather than full merging capabilities, making it suitable for use cases like code generators, scaffolding tools, or deployment scripts that need to prevent overwriting user modifications.","status":"active","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/SBoudrias/detect-conflict","tags":["javascript"],"install":[{"cmd":"npm install detect-conflict","lang":"bash","label":"npm"},{"cmd":"yarn add detect-conflict","lang":"bash","label":"yarn"},{"cmd":"pnpm add detect-conflict","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary `conflict` function is likely the default export in ESM environments.","wrong":"import { conflict } from 'detect-conflict';","symbol":"conflict","correct":"import conflict from 'detect-conflict';"},{"note":"CommonJS usage is demonstrated directly in the package's README.","symbol":"conflict","correct":"const conflict = require('detect-conflict');"},{"note":"While explicit type imports aren't typically needed for single-function libraries, this indicates how TypeScript would infer types if the library ships them.","symbol":"Type declarations","correct":"/// <reference types=\"detect-conflict\" />"}],"quickstart":{"code":"import conflict from 'detect-conflict';\nimport fs from 'fs';\nimport path from 'path';\n\nconst filePath = path.join(process.cwd(), 'example-file.js');\nconst existingContent = 'console.log(\"Hello world!\");\\nconst foo = 1;';\nconst newContent = 'console.log(\"Greetings!\");\\nconst foo = 2;'; // This will conflict on 'foo'\nconst safeContent = 'console.log(\"New message!\");\\nconst bar = 3;'; // This should not conflict\n\n// Ensure the file exists for demonstration\nfs.writeFileSync(filePath, existingContent, 'utf8');\n\nconsole.log(`Checking for conflict in ${filePath}`);\n\nconst isConflicting = conflict(filePath, newContent);\nconsole.log(`New content conflicts with existing: ${isConflicting}`);\n\nconst isConflictingSafe = conflict(filePath, safeContent);\nconsole.log(`Safe content conflicts with existing: ${isConflictingSafe}`);\n\n// Clean up the created file\nfs.unlinkSync(filePath);\n","lang":"javascript","description":"This example demonstrates how to use `detect-conflict` to check if new content for a file will conflict with the content currently on disk, showing both a conflicting and non-conflicting scenario."},"warnings":[{"fix":"Ensure that `filepath` always resolves to an actual file. Use `fs.statSync(filepath).isFile()` to verify before calling `conflict` if directory handling is uncertain.","message":"If the `filepath` argument points to a directory instead of a file, the `conflict` function will always return `true`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For non-UTF8 text files or binary files, read the file content into a Node.js `Buffer` using `fs.readFileSync(filepath)` (without specifying encoding) and pass the `Buffer` directly to the `conflict` function.","message":"When passing file `contents` as a string, `detect-conflict` assumes it is UTF-8 encoded. For other encodings, or for binary content, a `Buffer` must be passed.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade to `detect-conflict@1.0.1` or newer. Alternatively, ensure that the `contents` argument is always a `String` or `Buffer` and never `null`.","message":"Versions prior to 1.0.1 had a bug where passing `null` as the `contents` argument could lead to unexpected behavior or errors.","severity":"breaking","affected_versions":"<1.0.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"The library explicitly states that if `filepath` is a directory, it returns `true`. This error likely comes from *your* code trying to interact with the path as a file. Ensure `filepath` points to an actual file before passing it to `detect-conflict`, or handle the `true` return specifically for directories.","cause":"The `filepath` provided to `conflict` function resolves to a directory, but an attempt was made to open it as a file by underlying `fs` operations.","error":"Error: ENOTDIR: not a directory, open 'your-directory/file.js'"},{"fix":"Ensure you are importing the `conflict` function correctly. For CommonJS, use `const conflict = require('detect-conflict');`. For ESM, use `import conflict from 'detect-conflict';`.","cause":"This usually indicates an incorrect import statement or an attempt to call a non-existent function from the module.","error":"TypeError: conflict is not a function"}],"ecosystem":"npm"}