bendytree
bendytree

Reputation: 13629

Permissions To Read Windows File Across Network in NodeJS

I'm trying to read a text file using NodeJS on a Mac. The file is on a Windows computer which is on the same network.

var fs = require('fs');
var file = "smb://someserver/stuff/names.txt";

fs.readFile(file, 'utf8', function(err, data) {
  if (err) throw err;
  console.log(data);
});

When I run this code, I get:

> Error: ENOENT, open 'smb://someserver/stuff/names.txt'

I probably need to authenticate access to the file, but I haven't found any docs or modules that deal with this scenario. Is it possible to read/write files like this?

Update (10/22)

I've tried using UNC paths (ie \\someserver\stuff\names.txt) but I believe that is only part of the solution. Somehow I need to supply username/password/domain because this file requires special permission. Is it possible to supply permissions somehow? For example:

var fs = require('fs');
var file = '\\someserver\stuff\names.txt';
var credentials = { username:'mydomain/jsmith', password:'password' }; ???

fs.readFile(file, 'utf8', function(err, data) {
  if (err) throw err;
  console.log(data);
});

Upvotes: 8

Views: 4286

Answers (2)

Akshay Kumar
Akshay Kumar

Reputation: 21

You can use the 'node-cmd' to update the credentials first and then try to read/write to the folder using the unc path.

var cmd = require("node-cmd");
const data = cmd.runSync(
  "net use \\IP /user:username password"
);

// Now you do read / write using the fs
fs.readdir("\\IP")

Upvotes: 2

Sriharsha
Sriharsha

Reputation: 2443

You should use UNC paths if you are trying to access resources from other network drives.

var fs = require('fs');
var file = "\\someserver\stuff\names.txt";

fs.readFile(file, 'utf8', function(err, data) {
  if (err) throw err;
  console.log(data);
});

Upvotes: 2

Related Questions