Detect Conflict
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.
Common errors
-
Error: ENOTDIR: not a directory, open 'your-directory/file.js'
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.fixThe 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. -
TypeError: conflict is not a function
cause This usually indicates an incorrect import statement or an attempt to call a non-existent function from the module.fixEnsure you are importing the `conflict` function correctly. For CommonJS, use `const conflict = require('detect-conflict');`. For ESM, use `import conflict from 'detect-conflict';`.
Warnings
- gotcha If the `filepath` argument points to a directory instead of a file, the `conflict` function will always return `true`.
- gotcha 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.
- breaking Versions prior to 1.0.1 had a bug where passing `null` as the `contents` argument could lead to unexpected behavior or errors.
Install
-
npm install detect-conflict -
yarn add detect-conflict -
pnpm add detect-conflict
Imports
- conflict
import { conflict } from 'detect-conflict';import conflict from 'detect-conflict';
- conflict
const conflict = require('detect-conflict'); - Type declarations
/// <reference types="detect-conflict" />
Quickstart
import conflict from 'detect-conflict';
import fs from 'fs';
import path from 'path';
const filePath = path.join(process.cwd(), 'example-file.js');
const existingContent = 'console.log("Hello world!");\nconst foo = 1;';
const newContent = 'console.log("Greetings!");\nconst foo = 2;'; // This will conflict on 'foo'
const safeContent = 'console.log("New message!");\nconst bar = 3;'; // This should not conflict
// Ensure the file exists for demonstration
fs.writeFileSync(filePath, existingContent, 'utf8');
console.log(`Checking for conflict in ${filePath}`);
const isConflicting = conflict(filePath, newContent);
console.log(`New content conflicts with existing: ${isConflicting}`);
const isConflictingSafe = conflict(filePath, safeContent);
console.log(`Safe content conflicts with existing: ${isConflictingSafe}`);
// Clean up the created file
fs.unlinkSync(filePath);