The problem is that you are expecting the asynchronous call to populate the variable immediately, which isn't the case. The asynchronous call returns the result in the callback; the code following the asynchronous call is executed before the callback. You have two options to get what you want.
Your first option is to use the asynchronous method as you have, which is recommended, but put your "do something with fileNames here" code in the right place:
var fs = require('fs');
fs.readdir(dirPath, function(err, fileNames) {
if (err) { /* handle error */ }
//Do something with fileNames here
});
Your second option is to use the synchronous method, which already returns what you want:
var fs = require('fs');
var fileNames = fs.readdirSync(dirPath);
//Do something with fileNames here
Note that in general with Node, the async (first) method is preferred, however if this was in application startup code or a command-line script, you'd probably get away with the sync method.
Also note that neither of these uses underscore. The value (list
in the callback, or return value from sync method) is the list that you want, you don't need to populate into another variable.