ENOENT, no such file or directory but file exists

こ雲淡風輕ζ 提交于 2019-12-12 06:04:49

问题


I'm trying to build something using NodeJs on Intel Edison. I am using the plugin: https://github.com/blueimp/jQuery-File-Upload and the NodeJS Server side part.

However, I keep getting

fs.js:543
  return binding.rename(pathModule._makeLong(oldPath),
                 ^
Error: ENOENT, no such file or directory '/home/root/db/node_modules/blueimp-file-upload-node/tmp/8fa2946958c04ad8cb6def7b1e9dab01'
    at Object.fs.renameSync (fs.js:543:18)
    at IncomingForm.<anonymous> (/home/root/db/node_modules/blueimp-file-upload-node/server.js:248:16)
    at IncomingForm.EventEmitter.emit (events.js:98:17)
    at /home/root/db/node_modules/blueimp-file-upload-node/node_modules/formidable/lib/incoming_form.js:228:12
    at WriteStream.<anonymous> (/home/root/db/node_modules/blueimp-file-upload-node/node_modules/formidable/lib/file.js:70:5)
    at WriteStream.g (events.js:180:16)
    at WriteStream.EventEmitter.emit (events.js:117:20)
    at finishMaybe (_stream_writable.js:360:12)
    at afterWrite (_stream_writable.js:280:5)
    at onwrite (_stream_writable.js:270:7)

Whenever I try to upload something.The problem is that the file /home/root/db/node_modules/blueimp-file-upload-node/tmp/8fa2946958c04ad8cb6def7b1e9dab01 does indeed exist and I guess that I also have the permissions right in the folder.

I tried several things, but I am really stuck here, I cannot understand how can I get around this.


回答1:


After the error, is the file there by the old name or the new name? (I've had cases where two threads were inadvertenly started, and the second found the file already gone). Does the target directory exist? Any typos?

Here's what the linux manpage says about rename:

ENOENT The link named by oldpath does not exist; or, a directory component in newpath does not exist; or, oldpath or newpath is an empty string.

Edit: this is a nodejs bug, it reports that the source file is missing when it's actually the destination directory that's the problem. To reproduce from the command line:

% touch /tmp/file.txt
% node -p 'fs = require("fs"); fs.renameSync("/tmp/file.txt", "/nonesuch/file.txt");'

it prints:

Error: ENOENT, no such file or directory '/tmp/file.txt'
    at Object.fs.renameSync (fs.js:548:18)
    at [eval]:1:24
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:536:25)
    at startup (node.js:80:7)
    at node.js:906:3

Edit: here's a possible way to wrapper renameSync() to fix this (untested!)

var _origRename = fs.renameSync;
fs.renameSync = function(from, to) {
    try { _origRename(from, to) }
    catch (err) {
        if (err.stack.indexOf('ENOENT') < 0) throw err;
        try { fs.statSync(from) } catch (err2) { throw err }
        throw new Error("ENOENT, no such file or directory '" + to "'");
    }
}


来源:https://stackoverflow.com/questions/27207850/enoent-no-such-file-or-directory-but-file-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!