Error: EXDEV: cross-device link not permitted
2024.07.09
fs.rename
operation is attempting to move a file or directory across different devices (filesystems), which is not permitted.
To resolve this, use fs.copyFile
followed by fs.unlink
to effectively move the files by copying them to the new location and then deleting the originals.
// BEFORE
await fs.rename(baseDirPath, updatedDirPath);
// AFTER
async function copyDir(src, dest) {
await fs.mkdir(dest, { recursive: true });
const entries = await fs.readdir(src, { withFileTypes: true });
for (let entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
await copyDir(srcPath, destPath);
} else {
await fs.copyFile(srcPath, destPath);
}
}
}
await copyDir(baseDirPath, updatedDirPath);
await fs.rm(baseDirPath, { recursive: true, force: true });