Mapping Network Drive Node js

2.3k Views Asked by At

I'm new to node and I'm trying to get a list of files in a network drive using the directory-tree plugin but I'm getting a null return. Has anyone had this problem

var dirTree = require('directory-tree');

var tree = dirTree('\\\\10.10.1.6\\Images');
console.log(tree);
1

There are 1 best solutions below

0
On

There can be another way to find the contents in the directory which is using smb2 module. As network drive requires user credentials to access its contents therefore it is returning null. Also if we try to access it using file system of Node js then it may also return access denied. So to access the contents of network location we can use external node library i.e SMB2

An example for the same can be -

var smb2 = require('smb2');
var epConfig = {
smb2PdfGenerateConfig: {
    share: '\\\\10.10.1.6'
    , domain: 'domain'
    , username: 'username'
    , debug: false
    , password: 'password!'
    , autoCloseTimeout: 0
 }
}
var smb2Client = new smb2({
    share: epConfig.smb2PdfGenerateConfig.share,
    domain: epConfig.smb2PdfGenerateConfig.domain,
    username: epConfig.smb2PdfGenerateConfig.username,
    password: epConfig.smb2PdfGenerateConfig.password,
    debug: epConfig.smb2PdfGenerateConfig.debug,
    autoCloseTimeout: epConfig.smb2PdfGenerateConfig.autoCloseTimeout
});

smb2Client.readdir('Images', function(err, files){
    if(err) console.log("err",err);
    else{
       for(var i=0; i<files.length; i++) {
            console.log("file",files[i]);
       }
    }
});