ERR_FS_CP_SOCKET
Node.jsERRORCriticalFilesystemHIGH confidence

Cannot copy a Unix domain socket with fs.cp()

Production Risk

Low — only relevant when copying directories containing running service sockets.

What this means

Thrown when fs.cp() encounters a Unix domain socket file as the source. Socket files represent live IPC endpoints and cannot be meaningfully copied as their state is managed by the operating system, not stored as file content.

Why it happens
  1. 1Copying a directory tree that contains a Unix domain socket file
  2. 2Attempting to cp() a socket file path directly
How to reproduce

Triggered when cp() encounters a file of type socket during traversal.

trigger — this will error
trigger — this will error
const fs = require('fs');
// If src-dir contains a .sock file:
fs.cpSync('./src-dir', './dest-dir', { recursive: true }); // throws on the socket

expected output

Error [ERR_FS_CP_SOCKET]: Cannot copy a socket file: '/src-dir/app.sock'

Fix

Filter out socket files during copy

WHEN When copying directories that may contain socket files

Filter out socket files during copy
const fs = require('fs');
fs.cpSync('./src-dir', './dest-dir', {
  recursive: true,
  filter: (src) => {
    const stat = fs.statSync(src);
    return !stat.isSocket();
  },
});

Why this works

The filter callback excludes socket files from the copy operation.

Code examples
Triggerjs
const fs = require('fs');
// If src-dir contains a .sock file:
fs.cpSync('./src-dir', './dest-dir', { recursive: true }); // throws on the socket  // this triggers ERR_FS_CP_SOCKET
Handle in try/catchjs
try {
  // operation that may throw ERR_FS_CP_SOCKET
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_FS_CP_SOCKET') {
    console.error('ERR_FS_CP_SOCKET:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_fs_cp_socket(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Copy directories with socket files without a filter

Socket files cannot be duplicated; they represent live OS-managed IPC endpoints.

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