What does the "EXDEV: cross-device link not permitted" error mean?
Asked Answered
H

6

39

What does this error actually mean? What is a "cross-device link"?

It is mentioned on this libuv page but it doesn't give any details beyond "cross-device link not permitted".

Happen answered 4/4, 2017 at 11:40 Comment(0)
T
20

It is used for EXDEV on Linux:

See man rename manpage:

EXDEV oldpath and newpath are not on the same mounted filesystem. (Linux permits a filesystem to be mounted at multiple points, but rename() does not work across different mount points, even if the same filesystem is mounted on both.)

This error is also used when there is ERROR_NOT_SAME_DEVICE on Windows, see:

For more info see:

winerror.h 0x80070011 #define ERROR_NOT_SAME_DEVICE The system cannot move the file to a different disk drive.

Telluride answered 4/4, 2017 at 11:57 Comment(0)
W
42

It sounds like you're trying to rename a file across "device" (partition) boundaries.

Say that /tmp is a different partition than /. That means that you're not allowed to do this:

fs.rename('/tmp/myfile.txt', '/myfile.txt', ...)

(the same applies to fs.renameSync() as well, obviously)

If you want to do that, you need to first copy the file to its new location, and subsequently remove the old file. There are modules, like mv, that can help you with that.

Weinstock answered 4/4, 2017 at 11:53 Comment(5)
Using the combination of copyFile and unlink in the File System module should also do the trick.Marchand
@AmirSyafrudin true, but fs.copyFile didn't exist at the time I wrote this answer :DWeinstock
Indeed. Just wanted to add it in case any newbies such as myself stumbled upon your answer. :)Marchand
Code example for this solution here: https://mcmap.net/q/183125/-error-exdev-cross-device-link-not-permitted-rename-39-tmp-on-ubuntu-16-04-ltsCleveland
@SeanO with the additional remark that it reads the entire file into memory firstWeinstock
T
20

It is used for EXDEV on Linux:

See man rename manpage:

EXDEV oldpath and newpath are not on the same mounted filesystem. (Linux permits a filesystem to be mounted at multiple points, but rename() does not work across different mount points, even if the same filesystem is mounted on both.)

This error is also used when there is ERROR_NOT_SAME_DEVICE on Windows, see:

For more info see:

winerror.h 0x80070011 #define ERROR_NOT_SAME_DEVICE The system cannot move the file to a different disk drive.

Telluride answered 4/4, 2017 at 11:57 Comment(0)
G
17

I guess you are trying to copy a file from /temp folder since the form. I solved it coping, not renaming

        fs.copyFile(oldpath, newpath, function (err) {
            if (err) throw err;
            res.write('File uploaded and moved!');
            res.end();
        });
Goldschmidt answered 27/7, 2020 at 6:3 Comment(0)
C
3

Just for they guys who are using Linux this happens when your old path i.e. /tmp and new path are on different partitions or disks.

Cartie answered 10/9, 2019 at 13:13 Comment(1)
I'm using Linux Mint 21 and was trying this example [w3schools.com/nodejs/nodejs_uploadfiles.asp] and get the same error. I have 1 partition on my laptop.Vociferance
D
1

In my case i change the code from

for (const file of files) {
            const tempPath = file[1].filepath;
            await fs.rename(tempPath, targetPath + file[1].originalFilename);
        }

To the following code

  for (const file of files) {
      const tempPath = file[1].filepath;
      await fs.copyFile(tempPath, targetPath + file[1].originalFilename);
      await fs.rm(tempPath);
    }

and it work fine for me

I think the issue is we cannot move the temp file directly from node

Deangelis answered 17/2, 2023 at 6:51 Comment(0)
S
0

In my case using Node v16.20 I changed:

await fs.promises.rename(appPath, targetDir)

to

await fs.promises.cp(appPath, targetDir, {
    recursive: true,
})
await fs.promises.rm(appPath, {
    recursive: true,
})
Sweatshop answered 12/6, 2023 at 19:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.