Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js EXDEV rename error

I've been playing with some code I found in a book about Node.js. It is a simple app which uploads images.

It shows the EXDEV error (500 Error: EXDEV, rename).

Could someone give me a hint? Here's my code:

exports.submit = function(dir) {
    return function(req, res, next) {
        var img = req.files.photo.image;
        var name = req.body.photo.name || img.name;
        var path = join(dir, img.name);

        fs.rename(img.path, path, function (err) {
            if(err) return next(err);

            Photo.create({
                name: name,
                path: img.name
            }, function (err) {
                if(err) return next(err);
                res.redirect('/');
            });
        });
    };
};
like image 560
user2837851 Avatar asked Nov 29 '22 00:11

user2837851


2 Answers

Renaming files cannot be done cross-device. My guess is that your upload directory (which by default is /tmp) is on another partition/drive as your target directory (contained in the dir variable).

Some solutions:

  • configure the upload directory to be on the same partition/drive as your target directory; this depends on which module you're using to handle file uploads, express.bodyParser (and the module it uses, connect.multipart) accepts an uploadDir option that you can use;
  • before starting your Node app, set the TMPDIR environment variable to point to a temporary directory on the same partition/drive as your target directory. If you're using a Unix-type OS:

    env TMPDIR=/path/to/directory node app.js
    
  • instead of setting the environment variable from your shell, set it at the top of your Node app:

    process.env.TMPDIR = '/path/to/directory';
    
  • instead of renaming, use a module like mv that can work cross-device;
like image 129
robertklep Avatar answered Dec 12 '22 05:12

robertklep


Using Windows XP, I added to app.js:

process.env.TMPDIR = '.';  //new
like image 25
user2203991 Avatar answered Dec 12 '22 07:12

user2203991