Maximizing Node.js File System Operations with readdirp for Enhanced Performance

Getting Started with readdirp

In web development, especially with Node.js, effective file system operations are critical. readdirp is a highly efficient library that allows you to read directory contents recursively, making it a powerful tool for developers. This article will provide a comprehensive guide to using readdirp with multiple API examples and a complete application.

Installation

npm install readdirp

Basic Usage

To get started with readdirp, you need to require the module and use its main function.

const readdirp = require('readdirp');

Reading All Files in a Directory

 const readdirp = require('readdirp'); const settings = {
   root: 'path/to/directory',
   fileFilter: '*.js',
   directoryFilter: ['!node_modules']
};
readdirp(settings)
   .on('data', (entry) => {
       console.log(`${entry.path}`);
   })
   .on('warn', (warn) => {
       console.error('Warning: ', warn);
   })
   .on('error', (err) => {
       console.error('Error: ', err);
   });

Handling All Entries

 readdirp(settings)
   .on('data', (entry) => {
       console.log(`Read file: ${entry.path}, full path: ${entry.fullPath}`);
   })
   .on('end', () => {
       console.log('All files processed.');
   });

Advanced Use Cases

Filtering Files Using Glob Patterns

 const config = {
  root: 'src',
  fileFilter: ['*.js', '*.ts'],
  directoryFilter: ['!node_modules', '!.git']
};
readdirp(config)
  .on('data', (entry) => {
    console.log(`File: ${entry.basename}`);
  })
  .on('end', () => {
     console.log('Done reading.');
  });

Using Async Iterators

 async function readFiles() {
  for await (const entry of readdirp('path/to/directory')) {
    console.log(entry.path);
  }
} 
readFiles()
  .then(() => console.log('Done'))
  .catch((err) => console.error(err));

Real World App Example

Below is an example of how you can create a simple Node.js application that leverages the readdirp module to read files, filter on specific extensions, and process them:

 const fs = require('fs'); const readdirp = require('readdirp');
const processFiles = async () => {
  const settings = {
    root: './data',
    fileFilter: '*.txt'
  };

  for await (const entry of readdirp(settings)) {
    const content = fs.readFileSync(entry.fullPath, 'utf-8');
    console.log(`Processing file: ${entry.basename}`);
    console.log(content);
  }
};
processFiles()
  .then(() => console.log('All files processed.'))
  .catch((error) => console.error('Error processing files:', error));

Conclusion

readdirp is a versatile and powerful module for Node.js developers. It simplifies directory reading and file processing while offering robust filters and asynchronous iteration capabilities. The examples provided here only scratch the surface of what you can achieve with this excellent library.

Hash: 519490e4e242f8301aff7a38be621c386d2ad6edf7129bab8cbcbe32c172b008

Leave a Reply

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