Node js List All Directories in a Directory

Node js List All Directories in a Directory

To get list of all directories in a directory in node.js; In this tutorial, you will learn how to get list all directories in a directory in node js.

When you are working with Node.js, there are many situations where you want to know which folders exist inside a particular directory. For example, if you are building a file management system, you may need to display a list of folders for users to navigate. Or, if you’re writing a script to organize your files, you might want to find all subfolders within a folder. Essentially, listing directories in a directory is a fundamental task when dealing with files and directories in Node.js. Node.js provides tools to help you do this, whether you prefer to do it in a way that does not block your application (asynchronous) or in a more simple but non-blocking way (synchronous). This is an important skill when working with directories in Node.js, and is used in a variety of applications where file and folder manipulation is required.

Node.js, you can list all directories in a directory using built-in modules like fs and third-party libraries like fs-extra to make the process easier.

List All Directories in a Directory in Node.js

Here are three common approaches to get a list all directories in the directory in the node.js; as follows:

  • Method 1: Using the fs module (Asynchronous)
  • Method 2: Using the fs module (Synchronous)
  • Method 3: Using the fs-extra library

Method 1: Using the fs module (Asynchronous)

Using the fs module, you can list all directories in a directory asynchronously in node js. Here is an example that uses the fs.readdir() method to list all directories in the current directory:

const fs = require('fs');

// Define the directory path
const directoryPath = './myDirectory';

// Use fs.readdir to read the contents of the directory
fs.readdir(directoryPath, { withFileTypes: true }, (err, files) => {
  if (err) {
    console.error('Error reading directory:', err);
    return;
  }

  // Filter out the directories
  const directories = files.filter(file => file.isDirectory());

  // Print the directory names
  directories.forEach(directory => {
    console.log(directory.name);
  });
});

Method 2: Using the fs module (Synchronous)

Using the fs module, you can list all directories in a directory Synchronous in node js. Here is an example that uses the fs.readdirSync() method to list all directories in the current directory:

const fs = require('fs');

// Define the directory path
const directoryPath = './myDirectory';

try {
  const files = fs.readdirSync(directoryPath, { withFileTypes: true });
  
  // Filter out the directories
  const directories = files.filter(file => file.isDirectory());

  // Print the directory names
  directories.forEach(directory => {
    console.log(directory.name);
  });
} catch (err) {
  console.error('Error reading directory:', err);
}

Method 3: Using the fs-extra library

Using the fs-extra library, you can list all directories in a directory in node js. Here is an example that uses the fs-extra library method to list all directories in the current directory:

const fs = require('fs-extra');

// Define the directory path
const directoryPath = './myDirectory';

// Use fs.readdir to read the contents of the directory
fs.readdir(directoryPath, (err, files) => {
  if (err) {
    console.error('Error reading directory:', err);
    return;
  }

  // Filter out the directories
  const directories = files.filter(file => fs.statSync(file).isDirectory());

  // Print the directory names
  directories.forEach(directory => {
    console.log(directory);
  });
});

Conclusion

That’s it; you have learned three different methods for listing directories in a directory in Node.js.

Recommended Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *