glob-all
glob-all extends the functionality of the popular `node-glob` library by allowing developers to provide an array of multiple glob patterns instead of a single one. This enables more complex file selection logic, including robust pattern-level exclusion patterns (e.g., `!files/x/**`) that are evaluated sequentially, a feature not natively available in `node-glob`'s single-pattern API. It provides both asynchronous and synchronous APIs, mirroring `node-glob`'s interface. The current stable version is 3.3.1, with the latest release (in 2024) primarily focusing on dependency updates, indicating a maintenance-level release cadence. A key differentiator is its sophisticated handling of pattern precedence for duplicate files, where the result from a more precise pattern takes priority. Additionally, it optimizes performance across multiple patterns by internally leveraging `node-glob`'s `statCache` option, preventing redundant file system lookups. This makes it suitable for build systems and manifest generation where complex, ordered file selections are required.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'sync')
cause Attempting to use `glob-all`'s `sync` method (or the default `glob` function) with incorrect import syntax, often when mixing CommonJS `require` with ESM `import` expectations, or destructuring incorrectly.fixFor CommonJS, use `const glob = require('glob-all');` and then `glob.sync(...)` or `glob(...)`. For ESM, use `import glob from 'glob-all';` and then `glob.sync(...)` or `glob(...)`, ensuring your environment supports CJS-to-ESM interop. -
No files found for pattern(s)
cause Glob patterns are incorrect, paths are relative to an unexpected working directory, or exclusion patterns are too broad and accidentally exclude all intended files.fixVerify patterns against your actual file structure. Ensure your current working directory (CWD) is where you expect the patterns to resolve from. Test simpler patterns first to isolate issues, and remember that `glob-all` processes patterns sequentially, meaning exclusions can override previous inclusions.
Warnings
- gotcha Custom Exclude Pattern Behavior: `glob-all`'s `!` prefix on a pattern (e.g., `!files/x/**`) provides pattern-level exclusion, which differs from `node-glob`'s in-pattern `!` negation. Patterns are processed in the order they are provided.
- gotcha File Order and Precision: When a file matches multiple patterns, the result from the *more precise* pattern takes precedence. This can affect the final order of files in the output array.
- gotcha Filtering Directories from Results: By default, `glob-all` returns both files and directories. To exclude directories, you need an extra step.
Install
-
npm install glob-all -
yarn add glob-all -
pnpm add glob-all
Imports
- glob (async)
import { glob } from 'glob-all';const glob = require('glob-all'); glob(patterns, options, callback); - glob.sync
import { sync } from 'glob-all';const glob = require('glob-all'); const files = glob.sync(patterns, options); - default (ESM)
import * as globAll from 'glob-all';
import glob from 'glob-all';
Quickstart
const glob = require('glob-all');
const fs = require('fs');
const path = require('path');
// Create dummy files for demonstration
fs.mkdirSync('files/x', { recursive: true });
fs.writeFileSync('files/a.txt', 'a');
fs.writeFileSync('files/b.txt', 'b');
fs.writeFileSync('files/c.txt', 'c');
fs.writeFileSync('files/x/y.txt', 'y');
fs.writeFileSync('files/x/z.txt', 'z');
const files = glob.sync([
'files/**', // include all files/
'!files/x/**', // then, exclude files/x/
'files/x/z.txt' // then, reinclude files/x/z.txt
]);
console.log(files);
// Clean up dummy files
fs.rmSync('files', { recursive: true, force: true });