gitmyhub

node-glob

TypeScript ★ 8.7k updated 1mo ago

glob functionality for node.js

Node.js library for finding files using shell-style patterns like `**/*.js`, returns results as a promise, sync call, stream, or async iterator with optional file metadata.

TypeScriptNode.jsnpmsetup: easycomplexity 2/5

This is a Node.js library that lets your JavaScript or TypeScript code search for files using patterns instead of exact names. You give it something like **/*.js and it returns every JavaScript file in the folder tree, or *.{png,jpeg} to match multiple image types at once. It is published on npm simply as glob (not node-glob, which is a different, abandoned package).

The pattern language it uses is the same one that Unix shells like Bash understand: * matches any string within a single folder, ** crosses folder boundaries, ? matches a single character, and curly braces let you list alternatives. This makes it straightforward to describe file searches that would otherwise require writing a custom recursive directory walker from scratch.

The library gives you several ways to receive results. The main glob() function is async and returns a promise that resolves to a list of matching file paths. globSync() does the same thing but blocks until it finishes. If you have a large folder tree, globStream() hands back matches one at a time as a stream, so you do not have to wait for every result before you start processing. An iterator form is also available for looping through matches one by one in a for-await loop.

Beyond plain file path strings, glob can return richer path objects that include metadata like file size, permissions, and modification time. This lets you sort results by when a file was last changed or filter by read/write mode without making extra calls to the filesystem yourself. Ignore rules can be defined with patterns or with custom functions, giving you fine-grained control over which files and folders are skipped during the search walk.

The full README is longer than what was shown.

Where it fits