lib/flatten.js
function walkModules(dir, done) {

  var results = [];

  fs.readdir(dir, function(err, list) {
    if (err) return done(err);
    var pending = list.length;
    if (!pending) return done(null, results);

    list.forEach(function(file) {
      file = dir + path.sep + file;

      fs.stat(file, function(err, stat) {
        if (stat && stat.isDirectory()) {

          if (file.indexOf('node_modules') > -1) {
            walkModules(file, function(err, res) {
              results = results.concat(res);
              if (!--pending) done(null, results);
            });
          }
          else {
            if (!--pending) done(null, results);
          }

        } else {

          if (file.slice(-12) === 'package.json') {
            var parts = file.split(path.sep);
            if (parts[parts.length-3] === 'node_modules') {
              results.push(file);
            }
          }
          
          if (!--pending) done(null, results);
        }
      });
    });
  });
}
function walkModules(dir, done) {

  var results = [];

  fs.readdir(dir, function(err, list) {
    if (err) return done(err);
    var pending = list.length;
    if (!pending) return done(null, results);

    list.forEach(function(file) {
      file = dir + path.sep + file;

      fs.stat(file, function(err, stat) {
        if (stat && stat.isDirectory()) {

          if (file.indexOf('node_modules') > -1) {
            walkModules(file, function(err, res) {
              results = results.concat(res);
              if (!--pending) done(null, results);
            });
          }
          else {
            if (!--pending) done(null, results);
          }

        } else {

          if (file.slice(-12) === 'package.json') {
            var parts = file.split(path.sep);
            if (parts[parts.length-3] === 'node_modules') {
              results.push(file);
            }
          }
          
          if (!--pending) done(null, results);
        }
      });
    });
  });
}
function walkModules ( dir , done ) { var results = [ ] ; fs . readdir ( dir , function ( err , list ) { if ( err ) return done ( err ) ; var pending = list . length ; if ( ! pending ) return done ( null , results ) ; list . forEach ( function ( file ) { file = dir + path . sep + file ; fs . stat ( file , function ( err , stat ) { if ( stat && stat . isDirectory ( ) ) { if ( file . indexOf ( 'node_modules' ) > - 1 ) { walkModules ( file , function ( err , res ) { results = results . concat ( res ) ; if ( ! -- pending ) done ( null , results ) ; } ) ; } else { if ( ! -- pending ) done ( null , results ) ; } } else { if ( file . slice ( - 12 ) === 'package.json' ) { var parts = file . split ( path . sep ) ; if ( parts [ parts . length - 3 ] === 'node_modules' ) { results . push ( file ) ; } } if ( ! -- pending ) done ( null , results ) ; } } ) ; } ) ; } ) ; }
Walk module folder

Find folder node_modules, scan its folders, find package.json and store it, recursively

@param {String} dir Directory name
@param {Function} done this will contains array of package.json file
Walk module folder