fs.cp() encountered a file of unknown type
Production Risk
Low — most application directories do not contain special device files.
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.
- 1Copying a directory containing device files (block or character devices)
- 2Exotic filesystem file types that Node.js does not know how to copy
Triggered when cp() encounters a file type it cannot handle and does not have a specific error for.
const fs = require('fs');
// If src-dir contains a block device file:
fs.cpSync('./src-dir', './dest-dir', { recursive: true }); // may throwexpected 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
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.
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_UNKNOWNtry {
// 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
}
}// Validate inputs before calling the operation
function safe_err_fs_cp_unknown(...args) {
// validate args here
return performOperation(...args)
}✕ Use cp() on directories from system paths (/dev, /proc) without filtering
System directories contain device and proc files that cannot be copied.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev