a blog for those who code

Wednesday, 3 September 2014

How to get list of files in a directory in Nodejs

In this post we will show you how to get list of files in a directory in Nodejs. Before going into the code we will take you through some of the methods which we are using in this code.

fs.realpath(path, [cache], callback)

Using this method we will pass the path as our current directory, the callback gets two arguments for us (err, path). This method is used to get the path we are currently in.

fs.readdir(path, callback)

Using this method we will be reading contents of a directory. The callback gets two arguments (err, files) where files ia an array of the names of the files in the directory.

We will be using __dirname variable, which is a global variable in Nodejs and it represents the path to the Node.js script that is currently executing.

Code :
var fs = require('fs');
fs.realpath(__dirname, function(err, path) {
  if (err) {
  console.log('Path is : ' + path);
fs.readdir(__dirname, function(err, files) {
  if (err) return;
  files.forEach(function(f) {
  console.log('Files: ' + f);

So, If you have a directory named 'example' and have below contents in it

After running the above code you will get the output as

Please Like and Share the Blog, if you find it interesting and helpful.


  1. var fs = require("fs");
    function teste(p){
    fs.readdir(p, function (err, files) {
    if (err) throw err;
    return files;

    Print UNDEFINED, why??

    1. @Ivomar At first check that the path which you are sending is the correct path or not, try giving full path lets say "D:\SomeFolderName\". Other thing to be noted here is that fs.readdir(__dirname, function(err, files)) will read the directory not the individual files, so try specifying the directory name

    2. If instead of return files; I type console.log (files); it works perfectly. But I want a return.

    3. Because fs.readdir() is an async call, so the variables that are set in the callback are not available directly after the call, that's why when you are trying to get the files outside the function it is giving undefined. Try doing like this but just keep in mind that './client' specified in fs.readdir('./client', function (err, files)) is a directory not file.

      var fs = require("fs");
      var files = function (value) {
      fs.readdir('./client', function (err, files) {
      var fileDict = {};
      var fileIndex = 0;

      function getEachFile(i) {
      var fileName = files[i];

      fs.stat('./client/' + fileName, function (err, stat) {

      fileDict[fileIndex] = fileName;

      if (++i == files.length) {
      return value(fileDict);
      } else {
      return getEachFile(i);
      return getEachFile(0);

      files(function (value) { console.log(value); });

  2. OK, but there is no way to retrieve the value to use it in a structure next? while for example?

    1. Yes as far as I know there is a way to retrieve the value to use like the way I have shown in the above example just that you need to handle the asyn call.

  3. If you want recursive lists of files, and/or directories, and/or are interested in the file contents, check out node-dir https://github.com/fshost it makes dealing with these types of issues easy and has millions of users.

  4. I am new to programming. When I executed the code from the original poster, the names of all the files and subfolders got printout on screen. Then the path directory where this js code is located got display. Can someone explain to me why the fs.readdir() code segment got executed first and then followed by the code segment in fs.realpath()? I thought it should be the other way around as thefs.realpath() is listed first in the above code segment.
    So I have all the file names and subfolder names display and then the directory path of the


Contact Us


Email *

Message *


Best Hosting Plans

CC License