Node.js EXDEV rename error
javascript, node.js
Solution
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;
Problem
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('/'); }); }); }; }; ```