Tag Archives: [node.js] fs.renameSync()

[Node.js] fs.renameSync() Error: Error: ENOENT: no such file or directory, rename…

Learn node.js for the first time, follow the introduction of node and operate it again. In the last step, an error is encountered when uploading and displaying the picture

 fs.js:115
    throw err;
    ^
    Error: ENOENT: no such file or directory, rename 'C:\Users\catcher\AppData\Local\Temp\upload_16f7bede547980c767e1e031a3720f67' -> '/tmp/test.png'
    at Object.renameSync (fs.js:594:3)
    at C:\IdeaProjects\nodejs\requestHandlers.js:34:8
    at IncomingForm.<anonymous> (C:\IdeaProjects\nodejs\node_modules\formidable\lib\incoming_form.js:107:9)
    at IncomingForm.emit (events.js:182:13)
    at IncomingForm._maybeEnd (C:\IdeaProjects\nodejs\node_modules\formidable\lib\incoming_form.js:557:8)
    at C:\IdeaProjects\nodejs\node_modules\formidable\lib\incoming_form.js:238:12
    at WriteStream.<anonymous> (C:\IdeaProjects\nodejs\node_modules\formidable\lib\file.js:79:5)
    at Object.onceWrapper (events.js:273:13)
    at WriteStream.emit (events.js:182:13)
    at finishMaybe (_stream_writable.js:641:14)

find the corresponding code according to the error information

fs.renameSync(files.upload.path, "/tmp/test.png");

 

the first thought is that the code is a relative path, which makes it impossible to find the location of the file, so complete the path

fs.renameSync(files.upload.path,"C:/IdeaProjects/nodejs/tmp/test.png");

the same error is reported

Error: ENOENT: no such file or directory, rename 'C:\Users\catcher\AppData\Local\Temp\upload_cb107f6decde929aff2b86f5bfb3a330' -> 'C:/IdeaProjects/nodejs/tmp/test.png'

after careful observation, it is found that there may be left and right backslashes in the path name. That is, in windows, the path name spacer is the right backslash ‘\’, while in Linux and Mac OS, it is the left backslash ‘/’. So the code is changed to

fs.renameSync(files.upload.path,"C:\IdeaProjects\nodejs\tmp\test.png");

Still report an error

Error: ENOENT: no such file or directory, rename 'C:\Users\catcher\AppData\Local\Temp\upload_5b3fdf35fbb17a8579b5c2245f070543' -> 'C:IdeaProjects  odejs   mp      est.png'

 

the problem is that the right backslash is an escape character, so the path name should be

fs.renameSync(files.upload.path,"C:\\IdeaProjects\\nodejs\\tmp\\test.png");

Unfortunately, the error is still reported

Error: ENOENT: no such file or directory, rename 'C:\Users\catcher\AppData\Local\Temp\upload_d3cceb8f8c01ae2a796d1f356e91ae0f' -> 'C:\IdeaProjects\nodejs\tmp\test.png'

But the displayed path is right
after reading the error message again, no such file or directory, does the folder TMP need to be created manually( Yes, I thought this method would automatically create non-existent folders, so at the beginning, I tried to find the tmp folder through everything. After creating the tmp folder myself, I successfully uploaded pictures and previewed them
for file paths that have been changed repeatedly before, the following three methods can be used for personal testing:

 

fs.renameSync(files.upload.path, "./tmp/test.png"); //注意点号

fs.renameSync(files.upload.path, "C:\\IdeaProjects\\nodejs\\tmp\\test.png");

fs.renameSync(files.upload.path, "C:/IdeaProjects/nodejs/tmp/test.png");

Solution:

https://stackoverflow.com/questions/4568689/how-do-i-move-file-a-to-a-different-partition-or-device-in-node-js

 1  var fs = require('fs');
 2  //var util = require('util');
 3  var is = fs.createReadStream('source_file');
 4  var os = fs.createWriteStream('destination_file');
 5  is.pipe(os);
 6  is.on('end',function() {
 7     fs.unlinkSync('source_file');
 8  });
 9  /* node.js 0.6 and earlier you can use util.pump:
10  util.pump(is, os, function() {
11      fs.unlinkSync('source_file');
12  });
13  */