It's a very fast and efficient glob library for Node.js
fast-glob is a high-performance file system traversal library that finds files and directories matching glob patterns in Node.js applications. With over 88 million weekly downloads, it has become the de facto standard for pattern-based file matching in modern JavaScript tooling, powering build systems, bundlers, linters, and testing frameworks that need to efficiently scan large codebases.
The library solves the fundamental problem of locating files using wildcard patterns like **/*.js or src/**/test-*.ts without manually traversing directories or writing recursive filesystem logic. It's built on optimized internal dependencies including @nodelib/fs.walk for directory traversal, micromatch for pattern matching, and provides three distinct APIs (async, sync, stream) to fit different architectural needs.
Developers choose fast-glob when raw performance matters. It consistently outperforms alternatives like node-glob by 10-20% on large directory structures through optimized task generation that groups patterns by their base directories and minimizes redundant filesystem calls. The library handles cross-platform path differences, supports both positive and negative patterns for inclusion/exclusion logic, and provides TypeScript definitions out of the box.
Unlike native Node.js solutions or simpler wrappers, fast-glob exposes advanced controls like custom filesystem adapters, configurable traversal depth, dot file handling, and multiple output formats (paths, objects with dirent metadata, or full stat information). This makes it ideal for tooling that needs fine-grained control over file discovery without sacrificing speed.
import fg from 'fast-glob';
import { writeFile } from 'fs/promises';
// Find all TypeScript files excluding tests and node_modules
const sourceFiles = await fg([
'src/**/*.ts',
'!src/**/*.test.ts',
'!**/node_modules/**'
], {
dot: false,
ignore: ['dist', 'build']
});
console.log(`Found ${sourceFiles.length} source files`);
// Stream mode for memory-efficient large directory scans
const stream = fg.stream('assets/**/*.{png,jpg,svg}', {
objectMode: true,
stats: true
});
const imageManifest = [];
for await (const entry of stream) {
imageManifest.push({
path: entry.path,
size: entry.stats.size,
modified: entry.stats.mtime
});
}
await writeFile('image-manifest.json', JSON.stringify(imageManifest, null, 2));
// Sync API for configuration loading at startup
const configFiles = fg.sync(['.config/**/*.json', '!.config/cache/**'], {
absolute: true,
onlyFiles: true
});
configFiles.forEach(file => console.log(`Loading config: ${file}`));Build Tool File Discovery: Bundlers like Webpack, Rollup, and Vite use fast-glob to resolve entry points and assets matching patterns like src/**/*.{js,jsx,ts,tsx} across thousands of source files, enabling hot module replacement and tree-shaking workflows.
Linter and Formatter Integration: Tools like ESLint and Prettier leverage it to find all files matching configuration patterns (e.g., **/*.js excluding node_modules), processing negative patterns for .gitignore-style exclusions without manual directory walking.
Test Runner File Collection: Testing frameworks like Jest and Vitest use fast-glob to collect test files matching patterns such as **/__tests__/**/*.spec.ts or **/*.test.js, handling complex inclusion/exclusion rules defined in configuration files.
Static Site Generators: Frameworks like Next.js, Gatsby, and Astro scan content directories with patterns like content/**/*.{md,mdx} to build page routes and data layers, requiring fast traversal of potentially massive content repositories.
CLI Tool File Operations: Command-line utilities processing batches of files (image optimization, code migration, batch renaming) use fast-glob to match targets like assets/**/*.{png,jpg,webp} and stream results to avoid memory overhead on large datasets.
npm install fast-globpnpm add fast-globbun add fast-glob