3

The issue

I'm using the answer here Get all files recursively in directories NodejS however, when I assign it to a constant I'm trying to return the directories so I can have them available in an array, I have looked through globs documentation for an asnwer https://github.com/isaacs/node-glob, however I have had no successful results, I have tried using glob.end() and I have also console.log the folds variable below, I can see all the list of available methods and I have tried to use some of them with no success, does anyone know how to return the array like in the code example below? Thank you!

 const glob = require('glob');
 const src = 'assets';


function getFiles(err, res){
  if (err) {
   console.log('Error', err);
  } else {
  return res
  }
}

let folds =  glob(src + '/**/*', getFiles);
hjm
  • 1,733
  • 2
  • 16
  • 27

1 Answers1

6

I had the same problem.

glob() is asynchronous and that can make returning the end result somewhat complicated.

Use glob.sync() instead (where .sync stands for synchronous)

Example:

const files = glob.sync(src + '/**/*');
jaroslawj
  • 105
  • 2
  • 10