readdir-glob: Streaming Recursive Directory Reader with Glob Filtering

3.0.0 · active · verified Wed Apr 22

readdir-glob provides a memory-efficient, recursive file system directory reader with a streaming API, designed as an alternative to `node-glob` for scenarios requiring constant memory usage regardless of the file system size or the number of matched files. It leverages the `minimatch` library for glob pattern filtering. The current stable version is `3.0.0`, which requires Node.js 18 or later. The library ships with TypeScript types since v2.0.0 and supports both CommonJS and ESM. Its primary differentiator is its low, constant memory footprint, making it suitable for processing very large file systems where other glob libraries might exhaust memory. Releases appear to be driven by Node.js version updates and dependency security patches, with major versions aligning with Node.js LTS cycles.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `readdir-glob` to find all `.js` files recursively within a specified root directory, excluding `node_modules`, and streaming the results. It also shows basic error handling and how to use the `pause` and `resume` methods.

import readdirGlob from 'readdir-glob';
import path from 'path';

const rootDir = process.env.ROOT_DIR ?? '.'; // Specify a root directory or use current
const globber = readdirGlob(rootDir, { pattern: '**/*.js', ignore: 'node_modules/**', stat: true });

console.log(`Searching for .js files in '${rootDir}' (excluding node_modules)...`);

globber.on('match', match => {
    // match.relative: relative path of the matched file
    // match.absolute: absolute path of the matched file
    // match.stat: stat of the matched file (only if stat:true option is used)
    console.log(`Found: ${match.relative} (Absolute: ${match.absolute})`);
});

globber.on('error', err => {
    console.error('Fatal error during globbing:', err);
});

globber.on('end', () => {
    console.log('File search completed.');
});

// Example of pausing and resuming the search
setTimeout(() => {
  if (!globber.paused) {
    globber.pause();
    console.log('Search paused for 2 seconds.');
    setTimeout(() => {
      globber.resume();
      console.log('Search resumed.');
    }, 2000);
  }
}, 1000);

view raw JSON →