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

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

Leave a Comment