Jose Bernhardt Jose Bernhardt - 2 months ago 14
Javascript Question

Searching files in dir with Node.js

I'm currently trying to search for a few files in a specific folder on Windows using

node
and
grunt
.

I have a
grunt task
that has a function to read a dir with
JSON
files, but the problem is that when I run the task, the code to read the file doesn't do anything, everything else on that
grunt task
runs perfect, but that. I'm not sure if the reference for the path is correct, but I'm also using
path.normalize()
and it does not throws any error.

This is snippet of the code:

..// Some other code
var fs = require('fs'),
path = require("path");

grunt.registerTask('separate', function() {
var filePath = path.normalize("C:\Users\jbernhardt\Desktop\testkeeper\jenkinsReports");

fs.readdir(filePath, function(err, filenames) {

//This log doesn't show as it the function is not running
grunt.log.writeln("Testing");

if (err) {
grunt.log.writeln("Error");
return;
}
filenames.forEach(function(filename){
grunt.log.writeln("Testing");

});

});
...//Some more code below for the same task
}


Does anyone has an idea why this snippet of the code is being skipped when I run the task? I could probably be missing some basic stuffs. Thanks!

Answer

Try readdirSync and check if your function still not working. I guess your process is finished before the callback.

Comments