Directory traversal library that walks file system trees with multiple interface options including callback, event emitter, synchronous, and promise-based patterns.
—
The main walkdir function provides the foundational directory traversal functionality with multiple calling patterns and return types based on configuration options.
The primary entry point for directory traversal with flexible return types.
/**
* Walk a directory tree, emitting events or returning results based on options
* @param {string} path - Directory path to walk
* @param {WalkOptions|WalkEventListener} [options] - Walk options or event listener function
* @param {WalkEventListener} [eventListener] - Optional event listener function
* @returns {WalkEmitter|string[]|Object<string,fs.Stats>} - Return type depends on options
*/
function walkdir(path, options, eventListener);Return Behavior:
WalkEmitter for event-based handlingoptions.sync: true): Returns string[] or {[path]: stat} objectoptions.return_object: true): Returns object with path keys and stat valuesUsage Examples:
const walkdir = require('walkdir');
// Event emitter (async)
const emitter = walkdir('./directory');
emitter.on('path', (path, stat) => {
console.log('Found:', path);
});
// With callback
walkdir('./directory', (path, stat) => {
console.log('Path:', path, 'Type:', stat.isDirectory() ? 'dir' : 'file');
});
// With options and callback
walkdir('./directory', { max_depth: 2 }, (path, stat) => {
console.log('Found at depth ≤2:', path);
});
// Callback has access to emitter control methods via 'this'
walkdir('./project', function(path, stat) {
console.log('Processing:', path);
// Skip node_modules directories using this.ignore()
if (path.endsWith('node_modules')) {
this.ignore(path); // 'this' refers to the WalkEmitter
}
// Can also use this.pause(), this.resume(), this.end(), this.stop()
if (someCondition) {
this.end(); // Stop the walk from within the callback
}
});
// Sync mode - returns array
const paths = walkdir('./directory', { sync: true });
console.log('All paths:', paths);
// Sync mode - returns object
const stats = walkdir('./directory', { sync: true, return_object: true });
for (const [path, stat] of Object.entries(stats)) {
console.log(path, stat.size);
}The main walkdir function is available under multiple names for convenience.
/**
* All three are identical - just different names for the same function
*/
walkdir.find === walkdir.walk === walkdir; // true
// These all work exactly the same:
walkdir('./directory', options, callback);
walkdir.find('./directory', options, callback);
walkdir.walk('./directory', options, callback);The main function supports flexible parameter patterns:
// Path only - returns emitter
walkdir('./dir')
// Path + callback - returns emitter, calls callback for each path
walkdir('./dir', (path, stat) => {})
// Path + options - returns emitter or sync result based on options.sync
walkdir('./dir', { max_depth: 3 })
// Path + options + callback - full parameter set
walkdir('./dir', { max_depth: 3 }, (path, stat) => {})The function automatically detects whether the second parameter is an options object or callback function based on its type.
The core function handles errors differently based on mode:
error events for target path issues, fail events for nested path issuesInstall with Tessl CLI
npx tessl i tessl/npm-walkdir