ERR_FS_CP_UNKNOWN
Node.jsERRORCriticalFilesystemHIGH confidence

fs.cp() encountered a file of unknown type

Production Risk

Low — most application directories do not contain special device files.

What this means

Thrown when fs.cp() encounters a file of an unrecognised or unsupported type (not a regular file, directory, symlink, FIFO, or socket). This can occur on exotic filesystems or with unusual file types returned by the OS.

Why it happens
  1. 1Copying a directory containing device files (block or character devices)
  2. 2Exotic filesystem file types that Node.js does not know how to copy
How to reproduce

Triggered when cp() encounters a file type it cannot handle and does not have a specific error for.

trigger — this will error
trigger — this will error
const fs = require('fs');
// If src-dir contains a block device file:
fs.cpSync('./src-dir', './dest-dir', { recursive: true }); // may throw

expected output

Error [ERR_FS_CP_UNKNOWN]: Cannot copy an unknown file type: '/src-dir/device-file'

Fix

Use a filter to skip unsupported file types

WHEN When copying directories on filesystems with special files

Use a filter to skip unsupported file types
const fs = require('fs');
fs.cpSync('./src-dir', './dest-dir', {
  recursive: true,
  filter: (src) => {
    const stat = fs.lstatSync(src);
    return stat.isFile() || stat.isDirectory() || stat.isSymbolicLink();
  },
});

Why this works

Only copying known-good file types (regular files, directories, symlinks) avoids the unknown type error.

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

Use cp() on directories from system paths (/dev, /proc) without filtering

System directories contain device and proc files that cannot be copied.

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