I'm trying to move uploaded file from /tmp
to home
directory using NodeJS/ExpressJS:
fs.rename('/tmp/xxxxx', '/home/user/xxxxx', function(err){
if (err) res.json(err);
console.log('done renaming');
});
But it didn't work and no error encountered. But when new path is also in /tmp
, that will work.
Im using Ubuntu, home
is in different partition. Any fix?
Thanks
Yes, fs.rename does not move file between two different disks/partitions. This is the correct behaviour.
fs.rename
provides identical functionality torename(2)
in linux.Read the related issue posted here.
To get what you want, you would have to do something like this:
Updated ES6 solution ready to use with promises and async/await:
This example taken from: Node.js in Action
Another way is to use
fs.writeFile
.fs.unlink
in callback will remove the temp file from tmp directory.