title: SFTP modules in Node to download and delete files
body: <p>my task is (or better, should be) simple: to fetch the content of a directory in SFTP from a Node application, then download all the files in there and finally delete them remotely.</p>
  
  <p>Now, I've checked different modules. Very clean, in my opinion, is ssh2-sftp-client. I can use it like this:</p>
  
  <pre><code>let Client = require('ssh2-sftp-client');
  let sftp = new Client();
  
  sftp.connect({
      host: myhost,
      port: '22',
      username: myusername,
      password: mypassword
  }).then(() =&gt; {
      return sftp.list('/path/to/my/files');
  }).then((data) =&gt; {
  
      for(var i = 0 ; i &lt; data.length; i++) {
          sftp.get('/path/to/my/files/'+data[i].name); 
          }   
  }).catch((err) =&gt; {
      console.log(err, 'catch error');
  });
  </code></pre>
  
  <p>I can list all files and see the resulting jSon in console, but the .get command seems to do nothing and I have no idea what is wrong. From the documentation (<a href="https://www.npmjs.com/package/ssh2-sftp-client" rel="nofollow noreferrer">https://www.npmjs.com/package/ssh2-sftp-client</a>) you can get files simply like I'm doing. But:</p>
  
  <p>1) it doesn't seem to work
  2) how can I debug what's going on?
  3) if I have no chance to choose the local directory, will these files most probably be downloaded where the js is launched by node, right?</p>
  
  <p>So, how could I get the files actually downloaded and possibly deleted remotely if they were successfully downloaded with this module (or others if any)?</p>
  
  <p>Thanks,
  Fabio </p>