Problem:
While trying to rename a file using fs.move()
in NodeJS, you see an error message such as
Error: EXDEV: cross-device link not permitted, rename '/tmp/upload_8a8c71784abf9942b40c1359935b1997' -> 'myfile.pdf'
Solution:
This error occurs because you are moving a file from one drive (/tmp
in our example above) to another drive (the current directory in our example above). On most platforms, this can’t be done using a simple rename operation.
Instead, fs.copyFile()
the file.
For example, instead of
await fs.move('/tmp/upload_8a8c71784abf9942b40c1359935b1997', 'myfile.pdf')
first, copy the file:
await fs.copyFile('/tmp/upload_8a8c71784abf9942b40c1359935b1997', 'myfile.pdf')
and then – if desired – remove the source file to mirror the behaviour of os.move()
as closely as possible.
await fs.remove('/tmp/upload_8a8c71784abf9942b40c1359935b1997', 'myfile.pdf') await fs.unlink('/tmp/upload_8a8c71784abf9942b40c1359935b1997')