nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.44k stars 276 forks source link

Why NodeJS fs module won't read or write to visible folder shared on the network? #4390

Closed worknotalent closed 2 weeks ago

worknotalent commented 1 month ago

Details

I shared a folder on my network through the use of Windows UI.

I'm able to access this folder and edit its content through the File Explorer but problems arise when I try to do the same programmatically. Any fs functions will work on the hoster machine but on any secondary device it won't.

I had to pass the hoster session credentials when I first tried to access the folder from the secondary device so I'm guessing this folder is invisible to NodeJS for the same reason but I'm not sure, and if so I don't know how to pass credentials to NodeJS to make network folders accessible to it.

Node.js version

20.12.0

Example code

node:fs:2342 return binding.writeFileUtf8( ^

Error: UNKNOWN: unknown error, open '//DESKTOP-NAME/folder/test.txt' at Object.writeFileSync (node:fs:2342:20) at file:///C:/Users/me/some_folder/some_file.js:3:4 at ModuleJob.run (node:internal/modules/esm/module_job:222:25) at async ModuleLoader.import (node:internal/modules/esm/loader:323:24) at async loadESM (node:internal/process/esm_loader:28:7) at async handleMainPromise (node:internal/modules/run_main:113:12) { errno: -4094, code: 'UNKNOWN', syscall: 'open', path: '\\DESKTOP-NAME\folder\test.txt' }

Node.js v20.12.0

Operating system

Microsoft Windows [Version 10.0.19045.4291]

Scope

code

Module and version

fs

RedYetiDev commented 1 month ago

@nodejs/platform-windows does Node.js support network drives?

worknotalent commented 1 month ago

Someone on StackOverflow suggested to use a package called node-cmd to update the credentials first and then use the fs function but it still fails for me : import cmd from "node-cmd" cmd.runSync(net use \\DESKTOP-${device} /user:${username} ${password});

preveen-stack commented 1 month ago

Did you try escaping backslashes \\DESKTOP-NAME\folder\test.txt

mStirner commented 1 month ago

You cant use a smb/cifs share as path in node.js. Mount that network share as local resource like drive X: or what ever you want, and use then x:/path/to/wher-ever-you-want-to-go.

mStirner commented 1 month ago

@nodejs/platform-windows does Node.js support network drives?

Does node support nfs/linux network shares? No. You have to mount the network shared resources as local medium/device, and use that in node.js

tniessen commented 1 month ago

Does Node.js not support UNC paths on Windows? I was under the impression that any UNC path referring to a real file system (not an emulated file system or USB device or so) would work.

RedYetiDev commented 2 weeks ago

Thanks for the information!

It appears Node.js does not support the functionality you are attempting to use, so unfortunately it may not be possible to achieve the desired results.