Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6000337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:40:43+00:00 2026-05-23T00:40:43+00:00

So Im trying to use the nodejs express FS module to iterate a directory

  • 0

So Im trying to use the nodejs express FS module to iterate a directory in my app, store each filename in an array, which I can pass to my express view and iterate through the list, but Im struggling to do so. When I do a console.log within the files.forEach function loop, its printing the filename just fine, but as soon as I try to do anything such as:

var myfiles = [];
var fs = require('fs');
fs.readdir('./myfiles/', function (err, files) { if (err) throw err;
  files.forEach( function (file) {
    myfiles.push(file);
  });
});
console.log(myfiles);

it fails, just logs an empty object. So Im not sure exactly what is going on, I think it has to do with callback functions, but if someone could walk me through what Im doing wrong, and why its not working, (and how to make it work), it would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:40:44+00:00Added an answer on May 23, 2026 at 12:40 am

    The myfiles array is empty because the callback hasn’t been called before you call console.log().

    You’ll need to do something like:

    var fs = require('fs');
    fs.readdir('./myfiles/',function(err,files){
        if(err) throw err;
        files.forEach(function(file){
            // do something with each file HERE!
        });
     });
     // because trying to do something with files here won't work because
     // the callback hasn't fired yet.
    

    Remember, everything in node happens at the same time, in the sense that, unless you’re doing your processing inside your callbacks, you cannot guarantee asynchronous functions have completed yet.

    One way around this problem for you would be to use an EventEmitter:

    var fs=require('fs'),
        EventEmitter=require('events').EventEmitter,
        filesEE=new EventEmitter(),
        myfiles=[];
    
    // this event will be called when all files have been added to myfiles
    filesEE.on('files_ready',function(){
      console.dir(myfiles);
    });
    
    // read all files from current directory
    fs.readdir('.',function(err,files){
      if(err) throw err;
      files.forEach(function(file){
        myfiles.push(file);
      });
      filesEE.emit('files_ready'); // trigger files_ready event
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I am trying use a Java Uploader in a ROR app (for its ease
Trying to use an excpetion class which could provide location reference for XML parsing,
Trying to use this method (gist of which is use self.method_name in the FunnyHelper
I'm trying to use Dojo dnd Source(1.4.2) to create an interface where I can
I am trying to use xml documents to store data for a movie database,
I have a ASP.Net site, in which I'm trying to use Windows Authentication and
I'm running a simple web app backed by node.js, and I'm trying to use
I have a very basic ccnet.config file, in which I am trying to use
I'm trying to use NodeJS with a C program to http://en.wikipedia.org/wiki/Setvbuf for JS files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.