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 });

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response