Catalog
- problem
- Solve
- Ending
problem
Today, we'll look at the fs.renameSync error problem encountered when using services written with Node.js. There are two types. Next, we will introduce them separately.
First error type: source file does not exist
Familiar little partners know that the fs.renameSync method can rename files and indirectly move files during renaming. Today, when renaming a file using the fs.renameSync method, the following error message occurred:
<div class="layui-container mt-20"> <div class="layui-row layui-col-space20"> <div class="layui-col-md12"> <h1>ENOENT: no such file or directory, rename './movies/0.doc' -> './movies/0/0.doc0'</h1> <h2></h2> <pre>Error: ENOENT: no such file or directory, rename './movies/0.doc' -> './movies/0/0.doc0' at Object.fs.renameSync (fs.js:766:18) at exports.postupload (/root/boffmpeg/controller/admin.js:344:12) at Layer.handle [as handle_request] (/root/boffmpeg/node_modules/express/lib/router/layer.js:95:5) at next (/root/boffmpeg/node_modules/express/lib/router/route.js:137:13) at Array.<anonymous> (/root/boffmpeg/node_modules/multer/lib/make-middleware.js:53:37) at listener (/root/boffmpeg/node_modules/on-finished/index.js:169:15) at onFinish (/root/boffmpeg/node_modules/on-finished/index.js:100:5) at callback (/root/boffmpeg/node_modules/ee-first/index.js:55:10) at IncomingMessage.onevent (/root/boffmpeg/node_modules/ee-first/index.js:93:5) at emitNone (events.js:111:20) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1064:12) at _combinedTickCallback (internal/process/next_tick.js:139:11) at process._tickCallback (internal/process/next_tick.js:181:9) </pre> </div> </div> </div> Copy Code
With the error alert, we can know very clearly that the cause of the error is the absence of the source file.
The solution is also very simple, just find the real reason that the source file does not exist, the reason I encountered is that the path was written incorrectly. After modifying the path, the problem is solved.
Next, let's look at the second type of error.
Second error type: cross-disk migration
0|www | Error: EXDEV: cross-device link not permitted, rename './public/uploads/0.doc' -> './public/text/6181fed03b908702d75331ce/0.doc' 0|www | at Object.fs.renameSync (fs.js:766:18) 0|www | at /root/boffmpeg/controller/admin.js:275:16 0|www | at /root/boffmpeg/node_modules/mongoose/lib/model.js:4870:16 0|www | at /root/boffmpeg/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16 0|www | at /root/boffmpeg/node_modules/mongoose/lib/model.js:4893:21 0|www | at model.$__save.error (/root/boffmpeg/node_modules/mongoose/lib/model.js:502:7) 0|www | at /root/boffmpeg/node_modules/kareem/index.js:316:21 0|www | at next (/root/boffmpeg/node_modules/kareem/index.js:210:27) 0|www | at /root/boffmpeg/node_modules/kareem/index.js:183:9 0|www | at process.nextTick (/root/boffmpeg/node_modules/kareem/index.js:508:38) 0|www | at _combinedTickCallback (internal/process/next_tick.js:132:7) 0|www | at process._tickCallback (internal/process/next_tick.js:181:9) Copy Code
Cross-disk links are not allowed for error types, but since it is clear that both subdirectories belong to the same parent directory, this error makes me very confused. I believe the actual reason must not be cross-disk. Really, I thought I couldn't find it today.
But I still found out. The original fs.renameSync method did not create a non-existent directory by itself, that is, the reason for the above error is that the subdirectory 6181fed03b908702d75331ce could not be created.
Knowing this, the problem is solved and the related directories of the target files need to be created separately. An example of the modified code is as follows:
fs.mkdir('./public/text/'+id, { recursive: true }, (err) => { if (err) throw err; // todo }); Copy Code
Ending
There are other solutions, such as using the mv library to migrate files. How to use mv library to complete file renaming and migration Welcome to the following articles, which I'll cover here today. It's not too early, good night 😴.
Author's introduction: Hello, I am Data-Mining (liuzhen007), an audio-video technology enthusiast, as well as a CSDN blog expert, Huawei cloud community cloud enjoyment expert. Welcome to share more dry goods with me!
Â