How do I move file a to a different partition or device in Node.js?

Jeremy picture Jeremy · Dec 31, 2010 · Viewed 16.8k times · Source

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?

Answer

Chandra Sekar picture Chandra Sekar · Dec 31, 2010

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');
});
*/