node.jsubuntusmb

Node on Ubuntu: ENOENT error, but file does exist, I am sure


I have a JS script which attempts to copy CCTV files from one SMB location to another. All files are created in the same way are in essence the same. I am using glob to get a list of all files.

90% of files copy just fine, however randomly some fail to copy. I am using

fs.readFileSync(filesArr[i]);

Which results in

enter image description here

Note I am showing the source file location and the destination at the top.

When I then use Files in Ubuntu I can see each file and open it!

I can not understand is why it works most of the time.


Solution

  • I think the error description from nodejs fs module openSync method ENOENT is quite expressive. A simple search and another StackOverflow answer lead me to the reason, that your filename string is not encoded in the right way, you should escape special characters and in your case space characters, I think.

    Your filename from the error reads Failed Error: ENOENT: no such file or directory, open '/run/user/1000/gvfs/smb-share:server=192.168.150.2,share=qvrprorecording/File/Standard_Format/Ch001_Camera 001/Event002/2024-02-08/17/20240208-172457-172501.mp4'and there is an empty space between Camera and 001, which might lead to your error.

    Please try to add slashes / encode / escape your filename strings and report back if this does the trick.

    Edited: So I looked up nodejs fs module documentation which says to see the POSIX open(2) documentation for more details, where you can look up the ENOENT error and what it stands for.