Destination file already exists and force is not set
Production Risk
Low — deploy scripts should explicitly set force or check destination state.
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.
- 1Destination file already exists and { force: false } (the default) is in effect
- 2Running a copy operation without checking whether the destination is clean
Triggered when cp() encounters an existing destination and force is not enabled.
const fs = require('fs');
// 'output.txt' already exists
fs.cpSync('./input.txt', './output.txt'); // throws — file existsexpected 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
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
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.
const fs = require('fs');
// 'output.txt' already exists
fs.cpSync('./input.txt', './output.txt'); // throws — file exists // this triggers ERR_FS_CP_EEXISTtry {
// 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
}
}// Validate inputs before calling the operation
function safe_err_fs_cp_eexist(...args) {
// validate args here
return performOperation(...args)
}✕ Use cp() in automation without considering existing destinations
The default no-overwrite behaviour protects data but causes errors if not accounted for.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev