ERR_FS_CP_EEXIST
Node.jsERRORNotableFilesystemHIGH confidence

Destination file already exists and force is not set

Production Risk

Low — deploy scripts should explicitly set force or check destination state.

What this means

Thrown by fs.cp() when the destination path already exists and the force option is either false or not set. By default, cp() will not overwrite existing files to prevent accidental data loss.

Why it happens
  1. 1Destination file already exists and { force: false } (the default) is in effect
  2. 2Running a copy operation without checking whether the destination is clean
How to reproduce

Triggered when cp() encounters an existing destination and force is not enabled.

trigger — this will error
trigger — this will error
const fs = require('fs');
// 'output.txt' already exists
fs.cpSync('./input.txt', './output.txt'); // throws — file exists

expected output

Error [ERR_FS_CP_EEXIST]: File already exists: './output.txt'

Fix 1

Set force: true to overwrite existing files

WHEN When overwriting is intentional

Set force: true to overwrite existing files
fs.cpSync('./input.txt', './output.txt', { force: true });

Why this works

The force option allows cp() to overwrite existing files.

Fix 2

Check existence before copying

WHEN When you want to skip existing files

Check existence before copying
if (!fs.existsSync('./output.txt')) {
  fs.cpSync('./input.txt', './output.txt');
}

Why this works

Pre-checking prevents the error when you want to preserve existing files.

Code examples
Triggerjs
const fs = require('fs');
// 'output.txt' already exists
fs.cpSync('./input.txt', './output.txt'); // throws — file exists  // this triggers ERR_FS_CP_EEXIST
Handle in try/catchjs
try {
  // operation that may throw ERR_FS_CP_EEXIST
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_FS_CP_EEXIST') {
    console.error('ERR_FS_CP_EEXIST:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_fs_cp_eexist(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Use cp() in automation without considering existing destinations

The default no-overwrite behaviour protects data but causes errors if not accounted for.

Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors