Error: ENOENT when renaming a file in a node / express application

I try to upload a file to my node / express application and I get 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 application:

 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:

 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 use the body parser:

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

Any ideas? I tried changing the permissions of my entire application, but not the cube.

+8
file-io express
source share
2 answers

Have you verified that the destination path you are using exists? (maybe you mean app.get('loc') + "/uploads/" ...)

Oddly enough, when this happens (the source file exists and the destination directory is not specified), the error message you receive only indicates the source file ... Therefore, check if this is a problem.

Remember if you want to move the downloaded file to /a/b/c.txt , both /a and /a/b must already exist.

Also, if you need to move the file to another section, you will have to use something like this , or you will get an EXDEV error.

+13
source share

This probably won’t help the original poster, but if someone else encounters this problem and finds that the source and the recipient both seem to exist, still encounter this error, I hope this helps. When I ran into this problem, this is the exact problem I found - when I checked, both the source (full path to the file) and the destination (directory) were present, but renaming threw ENOENT.

In my case, the solution was to find out that I used the asynchronous version of the create directory function to create the target directory. As a result, during the rename attempt, the target directory did not exist yet, but as soon as I checked it, it was completed and the directory was there. Switching to the synchronous version of directory creation fixed the problem.

+1
source share

All Articles