Asynchronous glob function that returns a Promise resolving to an array of matched paths.
// Standard signature (returns strings)
function glob(
pattern: string | string[],
options?: GlobOptions
): Promise<string[]>
// With withFileTypes: true (returns Path objects)
function glob(
pattern: string | string[],
options: GlobOptionsWithFileTypesTrue
): Promise<Path[]>| Parameter | Type | Description |
|---|---|---|
pattern |
string | string[] |
A glob pattern or array of patterns to match |
options |
GlobOptions |
Optional configuration options |
Promise<string[]>- Array of matching file paths (default)Promise<Path[]>- Array of PathScurry Path objects (whenwithFileTypes: true)
import { glob } from 'globlin'
// Single pattern
const jsFiles = await glob('**/*.js')
console.log(jsFiles) // ['src/index.js', 'lib/utils.js', ...]
// Multiple patterns
const sourceFiles = await glob(['src/**/*.ts', 'lib/**/*.js'])
// With options
const files = await glob('**/*.txt', {
cwd: '/path/to/project',
ignore: ['node_modules/**']
})import { glob } from 'globlin'
// Returns Path objects instead of strings
const paths = await glob('**/*', { withFileTypes: true, stat: true })
for (const p of paths) {
console.log(p.fullpath()) // '/absolute/path/to/file'
console.log(p.relative()) // 'relative/path/to/file'
console.log(p.isFile()) // true/false
console.log(p.isDirectory()) // true/false
}import { glob } from 'globlin'
const controller = new AbortController()
// Cancel after 5 seconds
setTimeout(() => controller.abort(), 5000)
try {
const files = await glob('**/*', { signal: controller.signal })
} catch (err) {
if (err.name === 'AbortError') {
console.log('Operation was cancelled')
}
}import { glob } from 'globlin'
// Brace expansion
const assets = await glob('assets/**/*.{png,jpg,gif}')
// Character classes
const numbered = await glob('file[0-9].txt')
// Extglob patterns
const notTests = await glob('**/*.!(test).js')
// Negation with ignore
const files = await glob('**/*.js', {
ignore: ['**/*.test.js', '**/*.spec.js']
})import { glob } from 'globlin'
// Enable parallel directory walking (faster on HDDs/network drives)
const files = await glob('**/*.js', { parallel: true })
// Enable directory caching (faster for repeated operations)
const files1 = await glob('**/*.js', { cache: true })
const files2 = await glob('**/*.ts', { cache: true }) // Uses cached directory listingsinterface GlobOptions {
cwd?: string
root?: string
dot?: boolean
nobrace?: boolean
noglobstar?: boolean
noext?: boolean
nocase?: boolean
magicalBraces?: boolean
follow?: boolean
maxDepth?: number
matchBase?: boolean
absolute?: boolean
dotRelative?: boolean
mark?: boolean
nodir?: boolean
posix?: boolean
withFileTypes?: boolean
stat?: boolean
realpath?: boolean
ignore?: string | string[] | IgnorePattern
includeChildMatches?: boolean
platform?: 'linux' | 'darwin' | 'win32'
windowsPathsNoEscape?: boolean
signal?: AbortSignal
parallel?: boolean // Globlin-specific
cache?: boolean // Globlin-specific
}
interface GlobOptionsWithFileTypesTrue extends GlobOptions {
withFileTypes: true
}- Patterns without magic characters are resolved as literal paths
- Empty patterns or patterns that don't match return an empty array
- The function is fully compatible with glob v13's
glob()function - Use
globSync()for synchronous operation - Use
globStream()for streaming results to reduce memory usage
try {
const files = await glob('**/*.js', {
cwd: '/nonexistent/path'
})
// Returns empty array (doesn't throw)
} catch (err) {
// Errors are thrown for invalid options combinations
}Errors are thrown for:
withFileTypes+absolutecannot both be setmatchBase+noglobstarcannot both be set- Invalid pattern types (null, undefined)
- AbortSignal aborted before or during operation
- Use specific patterns:
src/**/*.jsis faster than**/*.js - Limit depth: Use
maxDepthwhen you know the maximum nesting - Use ignore patterns: Exclude
node_modules/**and other large directories - Enable parallel: On HDDs or network drives,
parallel: truecan help - Enable caching: When running multiple globs,
cache: truereuses directory listings
- globSync - Synchronous version
- globStream - Streaming version
- globIterate - Iterator version
- Glob class - Object-oriented interface
- Options - Full options reference