Jose Bernhardt Jose Bernhardt - 1 year ago 108
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


I have a
grunt task
that has a function to read a dir with
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
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

if (err) {


...//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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download