Node js Get List All Directories in a Directory Tutorial

In this tutorial, you will learn how to find and list all directories in a directory in node js using third party libraries and built in methods.

List All Directories in a Directory in Node.js

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

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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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