I'm trying to rename a file, and I think I'm going mad my code is simple, I check if the file exists, and if it exists, I rename it. Here is the code :
if (fs.existsSync(__dirname+"/"+req.files.file.path))
{
fs.rename(__dirname+"/"+req.files.file.path, __dirname+"/app/upload/portfolio/video/"+req.files.file.name, function(err) {
if (err)
throw err;
else
....
});
}
But I get this error (I've replaced the realpath by path/to/file) :
throw err;
^
Error: ENOENT, rename 'path/to/file/filename.mp4'
After a check, I see that the file exists (simply by copy/paste the filepath in the error)
What can be the reason of such an issue?
rename can fail with ENOENT not only if the source doesn’t exist, but also if the directory of the destination doesn’t exist. I suspect app/upload/portfolio/video/path/to/file does not exist.
Related
As you can see, there is a file at the path. But fs says no such file or directory. I can't understand why?
In another file, I can remove with the same code.
My boat.js file:
boat.findById(req.params.id,function(err, foundBoat) {
if(err){
console.log(err);
}else{
foundBoat.boatsFoto.forEach(function(path){
console.log(typeof(path));
fs.unlink("../public"+path,function(err){
if(err) throw err;
console.log('File deleted!');
});
});
}
});
And it is my error:
Error: ENOENT: no such file or directory, unlink '../public/uploads/akingokay/BoatsFoto/1524411110335kiralik-tekne.jpg'
at Error (native)
And you can see my file system
Can you try this instead:
fs.unlink("public"+path,function(err){
if(err) throw err;
console.log('File deleted!');
});
You should first install the path module via CLI:
npm install path --save
and use it:
fs.unlink(path.join("public/" + path, photo.id + ".jpg"), function(response) {
// handle the callback
});
Check if the path variable you are adding to the "/public" has a "/" at the beginning. If there is no/ seperating it it will treat the path as one.
Install and import path module. this helps you https://nodejs.org/api/path.html
Instead of "../public"+path use path.join("../", "public", path);
It depends where you host the server. If it is on a local machine then you will probably need to specify the complete path to the file(s) that you want to delete or manipulate. If it is on a web server that is live then you will need to specify the complete path to it.
on a "local machine" it might look something like this:
fs.unlink('/home/user/project/someothername/'+filename, err => {
// handler
});
Consider like you are in app.js file and give the relative path of deleting file to the fs.unlink('path/to/file'). it will work!
I don't understand one thing with fs.rename(), why this code always throw an error?
The file is correctly renamed but each time i got this error Error: ENOENT: no such file or directory, rename '/path/to/file/example.log' -> '/path/to/file/example_1.log'
var fs = require('fs');
var mv = fs.rename('example.log', 'example_1.log', function(err) {
if(err instanceof Error)
throw err;
});
When I run your exact code:
var fs = require('fs');
var mv = fs.rename('example.log', 'example_1.log', function(err) {
if(err instanceof Error)
throw err;
});
(the only difference is adding the fs require).
Then I get the error when I don't have the file in the current directory.
But when I create example.log and run the program then I don't get any error and the file is renamed.
Of course next time I get the error again because I no longer have the file with the original name.
Are you sure that it doesn't work for you?
By the way, instead of
if (err instanceof Error)
you may want to use:
if (err)
Just in case that your error is not the instance of Error but still is defined (this can be true while using some modules).
I am renaming files and are getting some weird behavior. It both works and throws an error.
This is the code:
var fs = require('fs');
var file = {
rename: function(from, to){
fs.rename(from, to, function (err) {
if (err) throw err;
console.log("[i] Renamed " + from + " to " + to);
});
}
}
When using it I get this console output:
main.js:1153 [i] Renamed E:\images\oldName.jpg to E:\images\newName.jpg
main.js:1152 Uncaught Error: ENOENT: no such file or directory, rename 'E:\images\oldName.jpg' -> 'E:\images\newName.jpg'
main.js:1152 (anonymous function)
fs.js:73 (anonymous function)
I don't understand what the problem is. The file is renamed anyway.
Also, it doesn't happen if the file is moved to another folder.
Why is this happening and do I have to worry about it?
The function is being called twice. That means the second time it's called the file is no longer there, leading to the 'no such file or directory'. I notice you're using windows which might also be part of the issue. Are you by any chance using fs.watch to call this function? You might be seeing an inherit problem in fs.watch under Windows duplicating events. If so, there's some example code that might help you.
I found this answer saying to use mv module.
But it doesn't seem it works if I want to move file to its parent directory.
For example I want to move all files on /tmp to /
var root = process.cwd(); // the directory where i run the script
mv(root + "/tmp", root, { mkdirp: true }, function(err) {
if(err) { return logger.error(err); }
});
and I got this error:
error: Error: EPERM, rename 'C:\Users\myusername\Documents\test\tmp'
I think it's because moving in NodeJS uses rename workaround and it can't rename it to the parent directory.
Any solution? Thanks
Nevermind, used fs-extra module here.
the fs.move actually works
I am trying to write a handler for file uploads in node.js using express framework. Following is the raw skeleton of it.
exports.handleUpload = function(req,res){
var temp_path = req.files.doc.path,
target_path = './uploads/' + req.files.doc.name ;
fs.rename(temp_path, target_path, function(err){
if(err){
console.log(err);
}
fs.unlink(temp_path, function(){
if(err){
console.log(err)
}
})
//Do stuff
})
}
However I get an error in the execution of renmae function occassionally(not always), especially with uploads of large files.
This is what the console caches from the error code
{ [Error: ENOENT, rename '/tmp/16368-19661hu.pptx'] errno: 34, code: 'ENOENT', path: '/tmp/16368-19661hu.pptx' }
From : https://github.com/joyent/node/blob/master/deps/uv/include/uv.h
XX(ENOENT, "no such file or directory")
The uploads/ directory does exist and permissions isn't an issue. Had it been so, it would have been failing each time, but it does not.
You're using the /tmp directory. The OS might be deleting the files since it can do so for the /tmp directory, hence the "randomness" of the issue. Use fs.exists before doing your other operations.