Glob
Match files using the patterns the shell uses.
The most correct and second fastest glob implementation in JavaScript. (See Comparison to Other JavaScript Glob Implementations at the bottom of this readme.)

Usage
Install with npm
Note the npm package name is not node-glob that's a different thing that was abandoned years ago. Just glob.
Note Glob patterns should always use / as a path separator, even on Windows systems, as \ is used to escape glob characters. If you wish to use \ as a path separator instead of using it as an escape character on Windows platforms, you may set windowsPathsNoEscape:true in the options. In this mode, special glob characters cannot be escaped, making it impossible to match a literal * ? and so on in filenames.
Command Line Interface
glob(pattern: string | string[], options?: GlobOptions) => Promise<string[] | Path[]>
glob(pattern: string | string[], options?: GlobOptions) => Promise<string[] | Path[]>Perform an asynchronous glob search for the pattern(s) specified. Returns Path objects if the withFileTypes option is set to true. See below for full options field desciptions.
globSync(pattern: string | string[], options?: GlobOptions) => string[] | Path[]
globSync(pattern: string | string[], options?: GlobOptions) => string[] | Path[]Synchronous form of glob().
Alias: glob.sync()
globIterate(pattern: string | string[], options?: GlobOptions) => AsyncGenerator<string>
globIterate(pattern: string | string[], options?: GlobOptions) => AsyncGenerator<string>Return an async iterator for walking glob pattern matches.
Alias: glob.iterate()
globIterateSync(pattern: string | string[], options?: GlobOptions) => Generator<string>
globIterateSync(pattern: string | string[], options?: GlobOptions) => Generator<string>Return a sync iterator for walking glob pattern matches.
Alias: glob.iterate.sync(), glob.sync.iterate()
globStream(pattern: string | string[], options?: GlobOptions) => Minipass<string | Path>
globStream(pattern: string | string[], options?: GlobOptions) => Minipass<string | Path>Return a stream that emits all the strings or Path objects and then emits end when completed.
Alias: glob.stream()
globStreamSync(pattern: string | string[], options?: GlobOptions) => Minipass<string | Path>
globStreamSync(pattern: string | string[], options?: GlobOptions) => Minipass<string | Path>Syncronous form of globStream(). Will read all the matches as fast as you consume them, even all in a single tick if you consume them immediately, but will still respond to backpressure if they're not consumed immediately.
Alias: glob.stream.sync(), glob.sync.stream()
hasMagic(pattern: string | string[], options?: GlobOptions) => boolean
hasMagic(pattern: string | string[], options?: GlobOptions) => booleanReturns true if the provided pattern contains any "magic" glob characters, given the options provided.
Brace expansion is not considered "magic" unless the magicalBraces option is set, as brace expansion just turns one string into an array of strings. So a pattern like 'x{a,b}y' would return false, because 'xay' and 'xby' both do not contain any magic glob characters, and it's treated the same as if you had called it on ['xay', 'xby']. When magicalBraces:true is in the options, brace expansion is treated as a pattern having magic.
escape(pattern: string, options?: GlobOptions) => string
escape(pattern: string, options?: GlobOptions) => stringEscape all magic characters in a glob pattern, so that it will only ever match literal strings
If the windowsPathsNoEscape option is used, then characters are escaped by wrapping in [], because a magic character wrapped in a character class can only be satisfied by that exact character.
Slashes (and backslashes in windowsPathsNoEscape mode) cannot be escaped or unescaped.
unescape(pattern: string, options?: GlobOptions) => string
unescape(pattern: string, options?: GlobOptions) => stringUn-escape a glob string that may contain some escaped characters.
If the windowsPathsNoEscape option is used, then square-brace escapes are removed, but not backslash escapes. For example, it will turn the string '[*]' into *, but it will not turn '\\*' into '*', because \ is a path separator in windowsPathsNoEscape mode.
When windowsPathsNoEscape is not set, then both brace escapes and backslash escapes are removed.
Slashes (and backslashes in windowsPathsNoEscape mode) cannot be escaped or unescaped.
Class Glob
GlobAn object that can perform glob pattern traversals.
const g = new Glob(pattern: string | string[], options: GlobOptions)
const g = new Glob(pattern: string | string[], options: GlobOptions)Options object is required.
See full options descriptions below.
Note that a previous Glob object can be passed as the GlobOptions to another Glob instantiation to re-use settings and caches with a new pattern.
Traversal functions can be called multiple times to run the walk again.
g.stream()
g.stream()Stream results asynchronously,
g.streamSync()
g.streamSync()Stream results synchronously.
g.iterate()
g.iterate()Default async iteration function. Returns an AsyncGenerator that iterates over the results.
g.iterateSync()
g.iterateSync()Default sync iteration function. Returns a Generator that iterates over the results.
g.walk()
g.walk()Returns a Promise that resolves to the results array.
g.walkSync()
g.walkSync()Returns a results array.
Properties
All options are stored as properties on the Glob object.
optsThe options provided to the constructor.patternsAn array of parsed immutablePatternobjects.
Options
Exported as GlobOptions TypeScript interface. A GlobOptions object may be provided to any of the exported methods, and must be provided to the Glob constructor.
All options are optional, boolean, and false by default, unless otherwise noted.
All resolved options are added to the Glob object as properties.
If you are running many glob operations, you can pass a Glob object as the options argument to a subsequent operation to share the previously loaded cache.
cwdString path orfile://string or URL object. The current working directory in which to search. Defaults toprocess.cwd(). See also: "Windows, CWDs, Drive Letters, and UNC Paths", below.This option may be either a string path or a
file://URL object or string.rootA string path resolved against thecwdoption, which is used as the starting point for absolute patterns that start with/, (but not drive letters or UNC paths on Windows).Note that this doesn't necessarily limit the walk to the
rootdirectory, and doesn't affect the cwd starting point for non-absolute patterns. A pattern containing..will still be able to traverse out of the root directory, if it is not an actual root directory on the filesystem, and any non-absolute patterns will be matched in thecwd. For example, the pattern/../*with{root:'/some/path'}will return all files in/some, not all files in/some/path. The pattern*with{root:'/some/path'}will return all the entries in the cwd, not the entries in/some/path.To start absolute and non-absolute patterns in the same path, you can use
{root:''}. However, be aware that on Windows systems, a pattern likex:/*or//host/share/*will always start in thex:/or//host/sharedirectory, regardless of therootsetting.windowsPathsNoEscapeUse\\as a path separator only, and never as an escape character. If set, all\\characters are replaced with/in the pattern.Note that this makes it impossible to match against paths containing literal glob pattern characters, but allows matching with patterns constructed using
path.join()andpath.resolve()on Windows platforms, mimicking the (buggy!) behavior of Glob v7 and before on Windows. Please use with caution, and be mindful of the caveat below about Windows paths. (For legacy reasons, this is also set ifallowWindowsEscapeis set to the exact valuefalse.)dotInclude.dotfiles in normal matches andglobstarmatches. Note that an explicit dot in a portion of the pattern will always match dot files.magicalBracesTreat brace expansion like{a,b}as a "magic" pattern. Has no effect if {@link nobrace} is set.Only has effect on the {@link hasMagic} function, no effect on glob pattern matching itself.
dotRelativePrepend all relative path strings with./(or.\on Windows).Without this option, returned relative paths are "bare", so instead of returning
'./foo/bar', they are returned as'foo/bar'.Relative patterns starting with
'../'are not prepended with./, even if this option is set.markAdd a/character to directory matches. Note that this requires additional stat calls.nobraceDo not expand{a,b}and{1..3}brace sets.noglobstarDo not match**against multiple filenames. (Ie, treat it as a normal*instead.)noextDo not match "extglob" patterns such as+(a|b).nocasePerform a case-insensitive match. This defaults totrueon macOS and Windows systems, andfalseon all others.Note
nocaseshould only be explicitly set when it is known that the filesystem's case sensitivity differs from the platform default. If settrueon case-sensitive file systems, orfalseon case-insensitive file systems, then the walk may return more or less results than expected.maxDepthSpecify a number to limit the depth of the directory traversal to this many levels below thecwd.matchBasePerform a basename-only match if the pattern does not contain any slash characters. That is,*.jswould be treated as equivalent to**/*.js, matching all js files in all directories.nodirDo not match directories, only files. (Note: to match only directories, put a/at the end of the pattern.)Note: when
followandnodirare both set, then symbolic links to directories are also omitted.statCalllstat()on all entries, whether required or not to determine whether it's a valid match. When used withwithFileTypes, this means that matches will include data such as modified time, permissions, and so on. Note that this will incur a performance cost due to the added system calls.ignorestring or string[], or an object withignoreandignoreChildrenmethods.If a string or string[] is provided, then this is treated as a glob pattern or array of glob patterns to exclude from matches. To ignore all children within a directory, as well as the entry itself, append
'/**'to the ignore pattern.Note
ignorepatterns are always indot:truemode, regardless of any other settings.If an object is provided that has
ignored(path)and/orchildrenIgnored(path)methods, then these methods will be called to determine whether any Path is a match or if its children should be traversed, respectively.followFollow symlinked directories when expanding**patterns. This can result in a lot of duplicate references in the presence of cyclic links, and make performance quite bad.By default, a
**in a pattern will follow 1 symbolic link if it is not the first item in the pattern, or none if it is the first item in the pattern, following the same behavior as Bash.Note: when
followandnodirare both set, then symbolic links to directories are also omitted.realpathSet to true to callfs.realpathon all of the results. In the case of an entry that cannot be resolved, the entry is omitted. This incurs a slight performance penalty, of course, because of the added system calls.absoluteSet to true to always receive absolute paths for matched files. Set tofalseto always receive relative paths for matched files.By default, when this option is not set, absolute paths are returned for patterns that are absolute, and otherwise paths are returned that are relative to the
cwdsetting.This does not make an extra system call to get the realpath, it only does string path resolution.
absolutemay not be used along withwithFileTypes.posixSet to true to use/as the path separator in returned results. On posix systems, this has no effect. On Windows systems, this will return/delimited path results, and absolute paths will be returned in their full resolved UNC path form, eg insted of'C:\\foo\\bar', it will return//?/C:/foo/bar.platformDefaults to value ofprocess.platformif available, or'linux'if not. Settingplatform:'win32'on non-Windows systems may cause strange behavior.withFileTypesReturn PathScurryPathobjects instead of strings. These are similar to a NodeJSDirentobject, but with additional methods and properties.withFileTypesmay not be used along withabsolute.signalAn AbortSignal which will cancel the Glob walk when triggered.fsAn override object to pass in custom filesystem methods. See PathScurry docs for what can be overridden.scurryA PathScurry object used to traverse the file system. If thenocaseoption is set explicitly, then any providedscurryobject must match this setting.includeChildMatchesboolean, defaulttrue. Do not match any children of any matches. For example, the pattern**\/foowould matcha/foo, but nota/foo/b/fooin this mode.This is especially useful for cases like "find all
node_modulesfolders, but not the ones innode_modules".In order to support this, the
Ignoreimplementation must support anadd(pattern: string)method. If using the defaultIgnoreclass, then this is fine, but if this is set tofalse, and a customIgnoreis provided that does not have anadd()method, then it will throw an error.Caveat It only ignores matches that would be a descendant of a previous match, and only if that descendant is matched after the ancestor is encountered. Since the file system walk happens in indeterminate order, it's possible that a match will already be added before its ancestor, if multiple or braced patterns are used.
For example:
It's best to only set this to
falseif you can be reasonably sure that no components of the pattern will potentially match one another's file system descendants, or if the occasional included child entry will not cause problems.
Glob Primer
Much more information about glob pattern expansion can be found by running man bash and searching for Pattern Matching.
"Globs" are the patterns you type when you do stuff like ls *.js on the command line, or put build/* in a .gitignore file.
Before parsing the path part patterns, braced sections are expanded into a set. Braced sections start with { and end with }, with 2 or more comma-delimited sections within. Braced sections may contain slash characters, so a{/b/c,bcd} would expand into a/b/c and abcd.
The following characters have special magic meaning when used in a path portion. With the exception of **, none of these match path separators (ie, / on all platforms, and \ on Windows).
*Matches 0 or more characters in a single path portion. When alone in a path portion, it must match at least 1 character. Ifdot:trueis not specified, then*will not match against a.character at the start of a path portion.?Matches 1 character. Ifdot:trueis not specified, then?will not match against a.character at the start of a path portion.[...]Matches a range of characters, similar to a RegExp range. If the first character of the range is!or^then it matches any character not in the range. If the first character is], then it will be considered the same as\], rather than the end of the character class.!(pattern|pattern|pattern)Matches anything that does not match any of the patterns provided. May not contain/characters. Similar to*, if alone in a path portion, then the path portion must have at least one character.?(pattern|pattern|pattern)Matches zero or one occurrence of the patterns provided. May not contain/characters.+(pattern|pattern|pattern)Matches one or more occurrences of the patterns provided. May not contain/characters.*(a|b|c)Matches zero or more occurrences of the patterns provided. May not contain/characters.@(pattern|pat*|pat?erN)Matches exactly one of the patterns provided. May not contain/characters.**If a "globstar" is alone in a path portion, then it matches zero or more directories and subdirectories searching for matches. It does not crawl symlinked directories, unless{follow:true}is passed in the options object. A pattern likea/b/**will only matcha/bif it is a directory. Follows 1 symbolic link if not the first item in the pattern, or 0 if it is the first item, unlessfollow:trueis set, in which case it follows all symbolic links.
[:class:] patterns are supported by this implementation, but [=c=] and [.symbol.] style class patterns are not.
Dots
If a file or directory path portion has a . as the first character, then it will not match any glob pattern unless that pattern's corresponding path part also has a . as its first character.
For example, the pattern a/.*/c would match the file at a/.b/c. However the pattern a/*/c would not, because * does not start with a dot character.
You can make glob treat dots as normal characters by setting dot:true in the options.
Basename Matching
If you set matchBase:true in the options, and the pattern has no slashes in it, then it will seek for any file anywhere in the tree with a matching basename. For example, *.js would match test/simple/basic.js.
Empty Sets
If no matching files are found, then an empty array is returned. This differs from the shell, where the pattern itself is returned. For example:
Comparisons to other fnmatch/glob implementations
While strict compliance with the existing standards is a worthwhile goal, some discrepancies exist between node-glob and other implementations, and are intentional.
The double-star character ** is supported by default, unless the noglobstar flag is set. This is supported in the manner of bsdglob and bash 5, where ** only has special significance if it is the only thing in a path part. That is, a/**/b will match a/x/y/b, but a/**b will not.
Note that symlinked directories are not traversed as part of a **, though their contents may match against subsequent portions of the pattern. This prevents infinite loops and duplicates and the like. You can force glob to traverse symlinks with ** by setting {follow:true} in the options.
There is no equivalent of the nonull option. A pattern that does not find any matches simply resolves to nothing. (An empty array, immediately ended stream, etc.)
If brace expansion is not disabled, then it is performed before any other interpretation of the glob pattern. Thus, a pattern like +(a|{b),c)}, which would not be valid in bash or zsh, is expanded first into the set of +(a|b) and +(a|c), and those patterns are checked for validity. Since those two are valid, matching proceeds.
The character class patterns [:class:] (posix standard named classes) style class patterns are supported and unicode-aware, but [=c=] (locale-specific character collation weight), and [.symbol.] (collating symbol), are not.
Repeated Slashes
Unlike Bash and zsh, repeated / are always coalesced into a single path separator.
Comments and Negation
Previously, this module let you mark a pattern as a "comment" if it started with a # character, or a "negated" pattern if it started with a ! character.
These options were deprecated in version 5, and removed in version 6.
To specify things that should not match, use the ignore option.
Windows
Please only use forward-slashes in glob expressions.
Though windows uses either / or \ as its path separator, only / characters are used by this glob implementation. You must use forward-slashes only in glob expressions. Back-slashes will always be interpreted as escape characters, not path separators.
Results from absolute patterns such as /foo/* are mounted onto the root setting using path.join. On windows, this will by default result in /foo/* matching C:\foo\bar.txt.
To automatically coerce all \ characters to / in pattern strings, thus making it impossible to escape literal glob characters, you may set the windowsPathsNoEscape option to true.
Windows, CWDs, Drive Letters, and UNC Paths
On posix systems, when a pattern starts with /, any cwd option is ignored, and the traversal starts at /, plus any non-magic path portions specified in the pattern.
On Windows systems, the behavior is similar, but the concept of an "absolute path" is somewhat more involved.
UNC Paths
A UNC path may be used as the start of a pattern on Windows platforms. For example, a pattern like: //?/x:/* will return all file entries in the root of the x: drive. A pattern like //ComputerName/Share/* will return all files in the associated share.
UNC path roots are always compared case insensitively.
Drive Letters
A pattern starting with a drive letter, like c:/*, will search in that drive, regardless of any cwd option provided.
If the pattern starts with /, and is not a UNC path, and there is an explicit cwd option set with a drive letter, then the drive letter in the cwd is used as the root of the directory traversal.
For example, glob('/tmp', { cwd: 'c:/any/thing' }) will return ['c:/tmp'] as the result.
If an explicit cwd option is not provided, and the pattern starts with /, then the traversal will run on the root of the drive provided as the cwd option. (That is, it is the result of path.resolve('/').)
Race Conditions
Glob searching, by its very nature, is susceptible to race conditions, since it relies on directory walking.
As a result, it is possible that a file that exists when glob looks for it may have been deleted or modified by the time it returns the result.
By design, this implementation caches all readdir calls that it makes, in order to cut down on system overhead. However, this also makes it even more susceptible to races, especially if the cache object is reused between glob calls.
Users are thus advised not to use a glob result as a guarantee of filesystem state in the face of rapid changes. For the vast majority of operations, this is never a problem.
See Also:
man shman bashPattern Matchingman 3 fnmatchman 5 gitignore
Glob Logo
Glob's logo was created by Tanya Brassie. Logo files can be found here.
The logo is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Contributing
Any change to behavior (including bugfixes) must come with a test.
Patches that fail tests or reduce performance will be rejected.
Comparison to Other JavaScript Glob Implementations
tl;dr
If you want glob matching that is as faithful as possible to Bash pattern expansion semantics, and as fast as possible within that constraint, use this module.
If you are reasonably sure that the patterns you will encounter are relatively simple, and want the absolutely fastest glob matcher out there, use fast-glob.
If you are reasonably sure that the patterns you will encounter are relatively simple, and want the convenience of automatically respecting
.gitignorefiles, use globby.
There are some other glob matcher libraries on npm, but these three are (in my opinion, as of 2023) the best.
full explanation
Every library reflects a set of opinions and priorities in the trade-offs it makes. Other than this library, I can personally recommend both globby and fast-glob, though they differ in their benefits and drawbacks.
Both have very nice APIs and are reasonably fast.
fast-glob is, as far as I am aware, the fastest glob implementation in JavaScript today. However, there are many cases where the choices that fast-glob makes in pursuit of speed mean that its results differ from the results returned by Bash and other sh-like shells, which may be surprising.
In my testing, fast-glob is around 10-20% faster than this module when walking over 200k files nested 4 directories deep1. However, there are some inconsistencies with Bash matching behavior that this module does not suffer from:
**only matches files, not directories..path portions are not handled unless they appear at the start of the pattern./!(<pattern>)will not match any files that start with<pattern>, even if they do not match<pattern>. For example,!(9).txtwill not match9999.txt.Some brace patterns in the middle of a pattern will result in failing to find certain matches.
Extglob patterns are allowed to contain
/characters.
Globby exhibits all of the same pattern semantics as fast-glob, (as it is a wrapper around fast-glob) and is slightly slower than node-glob (by about 10-20% in the benchmark test set, or in other words, anywhere from 20-50% slower than fast-glob). However, it adds some API conveniences that may be worth the costs.
Support for
.gitignoreand other ignore files.Support for negated globs (ie, patterns starting with
!rather than using a separateignoreoption).
The priority of this module is "correctness" in the sense of performing a glob pattern expansion as faithfully as possible to the behavior of Bash and other sh-like shells, with as much speed as possible.
Note that prior versions of node-glob are not on this list. Former versions of this module are far too slow for any cases where performance matters at all, and were designed with APIs that are extremely dated by current JavaScript standards.
[1]: In the cases where this module returns results and fast-glob doesn't, it's even faster, of course.

Benchmark Results
First number is time, smaller is better.
Second number is the count of results returned.
Last updated