I'm trying to move a file from one partition to another in a Node.js script. When I used fs.renameSync
I received Error: EXDEV, Cross-device link
. I'd copy it over and delete the original, but I don't see a command to copy files either. How can this be done?

- 1
- 85
- 340
- 366
5 Answers
You need to copy and unlink when moving files across different partitions. Try this,
var fs = require('fs');
//var util = require('util');
var is = fs.createReadStream('source_file');
var os = fs.createWriteStream('destination_file');
is.pipe(os);
is.on('end',function() {
fs.unlinkSync('source_file');
});
/* node.js 0.6 and earlier you can use util.pump:
util.pump(is, os, function() {
fs.unlinkSync('source_file');
});
*/

- 161
- 1
- 9

- 10,683
- 3
- 39
- 54
-
And, unlike the other solutions, cleans up the source directory. – Paul Beusterien Oct 03 '11 at 04:17
-
1Isn't this very resource intensive? – Thomaschaaf Sep 01 '12 at 15:00
-
7util.pump is deprecated. use is.pipe(os) and then listen to the 'end' event on the output stream. – andrewrk Oct 06 '12 at 05:54
-
1For me it was the input stream that was sending the `end` event not the output stream. So: `is.pipe(os)` and `is.on('end', function(){});` worked. – Barış Uşaklı May 15 '13 at 15:45
-
2Why do you use `fs.unlinkSync` and not `fs.unlink`? – EaterOfCode Jul 05 '13 at 12:49
-
Is possible to move file from local machine to server? – Hulk1991 Jul 31 '13 at 05:30
-
this isn't a suitable replacement for fs.renameSync, because it isn't synchronous. – Michael Sep 03 '17 at 22:17
One more solution to the problem.
There's a package called fs.extra written by "coolaj86" on npm.
You use it like so:
npm install fs.extra
fs = require ('fs.extra');
fs.move ('foo.txt', 'bar.txt', function (err) {
if (err) { throw err; }
console.log ("Moved 'foo.txt' to 'bar.txt'");
});
I've read the source code for this thing. It attempts to do a standard fs.rename()
then, if it fails, it does a copy and deletes the original using the same util.pump()
that @chandru uses.

- 1,383
- 11
- 15
-
By the way, I've spoken to coolaj86 on github and he is aware that util.pump() is deprecated in node 0.10 and will be fixing that shortly. – erapert May 27 '13 at 00:34
-
The author of the fs-extra package is jprichardson and coolaj86 is a contributor (see https://github.com/jprichardson/node-fs-extra for more). – davidbourguignon Aug 05 '13 at 11:45
I know this is already answered, but I ran across a similar problem and ended up with something along the lines of:
require('child_process').spawn('cp', ['-r', source, destination])
What this does is call the command cp
("copy"). Since we're stepping outside of Node.js, this command needs to be supported by your system.
I know it's not the most elegant, but it did what I needed :)
to import the module and save it to your package.json file
npm install mv --save
then use it like so:
var mv = require('mv');
mv('source_file', 'destination_file', function (err) {
if (err) {
throw err;
}
console.log('file moved successfully');
});
-
this isn't a suitable replacement for fs.renameSync, because it isn't synchronous. https://github.com/andrewrk/node-mv/issues/9 – Michael Sep 03 '17 at 22:17
I made a Node.js module that just handles it for you. You don't have to think about whether it's going to be moved within the same partition or not. It's the fastest solution available, as it uses the recent fs.copyFile()
Node.js API to copy the file when moving to a different partition/disk.
Just install move-file
:
$ npm install move-file
Then use it like this:
const moveFile = require('move-file');
(async () => {
await moveFile(fromPath, toPath);
console.log('File moved');
})();

- 62,972
- 39
- 168
- 232