Skip to content

Commit cce9707

Browse files
committed
doc: document glob pattern syntax for fs.glob
The fs.glob API was marked stable in v24 but the pattern parameter lacked any description of supported syntax. Add a pattern syntax reference table, platform behavior notes, a cross-reference to path.matchesGlob(), and an Array.fromAsync example. Also add pattern syntax cross-references from fs.glob() (callback) and fs.globSync(). Refs: #58981 Continues the work from #58988
1 parent aac5b68 commit cce9707

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

doc/api/fs.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ changes:
11061106
description: Add support for `withFileTypes` as an option.
11071107
-->
11081108
1109-
* `pattern` {string|string\[]}
1109+
* `pattern` {string|string\[]} One or more glob patterns to match against.
11101110
* `options` {Object}
11111111
* `cwd` {string|URL} current working directory. **Default:** `process.cwd()`
11121112
* `exclude` {Function|string\[]} Function to filter out files/directories or a
@@ -1120,6 +1120,28 @@ changes:
11201120
* Returns: {AsyncIterator} An AsyncIterator that yields the paths of files
11211121
that match the pattern.
11221122
1123+
Retrieves file paths matching the specified glob `pattern`. The pattern
1124+
syntax is based on [`minimatch`][] and supports the following constructs:
1125+
1126+
| Pattern | Description |
1127+
| --------- | ------------------------------------------------------------------------------------------------------------------------------- |
1128+
| `*` | Matches any number of characters in a single path segment, excluding path separators. |
1129+
| `?` | Matches exactly one character, excluding path separators. |
1130+
| `[abc]` | Matches any one character inside the brackets. Ranges like `[a-z]` and negation like `[!abc]` or `[^abc]` are supported. |
1131+
| `**` | Matches zero or more path segments. When used as a full segment (e.g., `a/**/b`), it traverses directories recursively. |
1132+
| `{a,b,c}` | Brace expansion. Matches any of the comma-separated alternatives. Nested braces and numeric ranges like `{1..5}` are supported. |
1133+
1134+
Pattern matching is case-insensitive on macOS and Windows, and
1135+
case-sensitive on Linux. Negation patterns (`!pattern`) and `#` comments
1136+
are not supported. To exclude paths, use the `exclude` option instead.
1137+
1138+
When an array of patterns is provided, a file is included if it matches
1139+
any of the patterns.
1140+
1141+
To check whether a single path matches a glob pattern without traversing the
1142+
file system, use [`path.matchesGlob()`][]. The `path.matchesGlob()` method
1143+
does not support the `exclude` option.
1144+
11231145
```mjs
11241146
import { glob } from 'node:fs/promises';
11251147

@@ -1136,6 +1158,15 @@ const { glob } = require('node:fs/promises');
11361158
})();
11371159
```
11381160
1161+
Collecting all results into an array:
1162+
1163+
```mjs
1164+
import { glob } from 'node:fs/promises';
1165+
1166+
const files = await Array.fromAsync(glob('**/*.js'));
1167+
console.log(files);
1168+
```
1169+
11391170
### `fsPromises.lchmod(path, mode)`
11401171
11411172
<!-- YAML
@@ -3219,7 +3250,8 @@ changes:
32193250
description: Add support for `withFileTypes` as an option.
32203251
-->
32213252
3222-
* `pattern` {string|string\[]}
3253+
* `pattern` {string|string\[]} One or more glob patterns to match against.
3254+
See [`fsPromises.glob()`][] for supported pattern syntax.
32233255
32243256
* `options` {Object}
32253257
* `cwd` {string|URL} current working directory. **Default:** `process.cwd()`
@@ -5788,7 +5820,8 @@ changes:
57885820
description: Add support for `withFileTypes` as an option.
57895821
-->
57905822
5791-
* `pattern` {string|string\[]}
5823+
* `pattern` {string|string\[]} One or more glob patterns to match against.
5824+
See [`fsPromises.glob()`][] for supported pattern syntax.
57925825
* `options` {Object}
57935826
* `cwd` {string|URL} current working directory. **Default:** `process.cwd()`
57945827
* `exclude` {Function|string\[]} Function to filter out files/directories or a
@@ -8778,6 +8811,7 @@ the file contents.
87788811
[`fs.writev()`]: #fswritevfd-buffers-position-callback
87798812
[`fsPromises.access()`]: #fspromisesaccesspath-mode
87808813
[`fsPromises.copyFile()`]: #fspromisescopyfilesrc-dest-mode
8814+
[`fsPromises.glob()`]: #fspromisesglobpattern-options
87818815
[`fsPromises.mkdtemp()`]: #fspromisesmkdtempprefix-options
87828816
[`fsPromises.open()`]: #fspromisesopenpath-flags-mode
87838817
[`fsPromises.opendir()`]: #fspromisesopendirpath-options
@@ -8787,6 +8821,7 @@ the file contents.
87878821
[`inotify(7)`]: https://man7.org/linux/man-pages/man7/inotify.7.html
87888822
[`kqueue(2)`]: https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
87898823
[`minimatch`]: https://github.com/isaacs/minimatch
8824+
[`path.matchesGlob()`]: path.md#pathmatchesglobpath-pattern
87908825
[`util.promisify()`]: util.md#utilpromisifyoriginal
87918826
[bigints]: https://tc39.github.io/proposal-bigint
87928827
[caveats]: #caveats

0 commit comments

Comments
 (0)