Error: ENOENT when renaming file in node/express app

Viewed 11644

I am attempting to upload a file in my node/express app, and I am getting the following error:

{ [Error: ENOENT, rename '/tmp/64124a9886fdb03f1faee159bc533776']
  errno: 34,
  code: 'ENOENT',
  path: '/tmp/64124a9886fdb03f1faee159bc533776' }

/home/frankie/Projects/LP/routes/manager/deliverables.js:51
                            throw err;
                                  ^
Error: ENOENT, rename '/tmp/64124a9886fdb03f1faee159bc533776'

Here is the relevant code from my app:

if (req.files.file.name !== '' && req.files.file.size !== 0) {
    // this will move the uploaded file from the tmp folder to the uploads folder
    fs.rename(req.files.file.path, app.get('loc') +  "uploads/" + name + "-" + id + "/" + req.files.file.name, function (err) {
        if (err) throw err;

When I check what is in /tmp the file is there:

fiega@fiega:/tmp$ ll
total 56
drwxrwxrwt 12 root    root    4096 Dec 12 11:33 ./
drwxr-xr-x 23 root    root    4096 Sep 27 22:54 ../
-rw-rw-r--  1 fiega   fiega    903 Dec 12 11:33 13a26570f87297fd7f61785ef7d8772b

This is how I am using body parser:

app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());

Any ideas? I have tried changing the permissions of my entire my app but no dice.

2 Answers

Probably won't help the original poster, but in case somebody else runs into this issue and finds that the source and destination both appear to exist yet are running into this error, hopefully this will help. When I ran into this issue this is the exact problem I found - when I checked, both the source (full path to file) and destination (directory) was present, yet the rename was throwing ENOENT.

In my case the solution was to recognize that I was using the asynchronous version of the directory creation function to create the destination directory. As a result, at the time the rename was attempt the destination directory did not yet exist, but as soon as I checked it had completed and the directory was there. Switching to the synchronous version of directory creation fixed the problem.

Related