Skip to main content

Function: findFilesRecursively()

findFilesRecursively(path: PathLike, predicate: (filePath: string) => boolean): AsyncIterableIterator<string>

Parameters

ParameterTypeDescription
pathPathLikeThe path in which to find files.
predicate(filePath: string) => booleanA predicate function receives the path as a parameter. Truthy values will have the path included, falsey values will have the file excluded.

Returns

AsyncIterableIterator<string>

An AsyncIterableIterator of all the files. To loop over these use for await (const file of findFilesRecursively(path, predicate)) {}

Examples

// With CommonJS: To find all files ending with `.ts` in the src directory:
const path = require('node:path');

for await (const file of findFilesRecursively(path.join(__dirname, 'src'), (filePath) => filePath.endsWith('.ts'))) {
console.log(file);
}
// With ESM: To find all files ending with `.ts` in the src directory:
for await (const file of findFilesRecursively(new URL('src', import.meta.url), (filePath) => filePath.endsWith('.ts'))) {
console.log(file);
}

Source

lib/findFilesRecursively.ts:30