Node.js Reference ยท fs and fs/promises
Node.js fs.glob() method
Reference for fs.glob() in Node.js: find files with patterns like **/*.js, the cwd, exclude and withFileTypes options, and the callback, sync and async forms.
fs.glob(pattern, callback) finds files and directories whose paths match a glob pattern, such as src/**/*.js. It is the built-in answer to "give me all the files of this kind", which used to need an npm package.
There are three forms. fs.glob() calls back with an array of matches, fs.globSync() returns the array, and fsPromises.glob() returns an async iterator that hands you one match at a time, which is better when the tree is large. The order of matches is not guaranteed, so sort them when order matters.
import { globSync } from "node:fs"; const files = globSync("src/**/*.js"); console.log(files.sort());
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
fs.glob(pattern[, options], callback) fs.globSync(pattern[, options]): string[] | fs.Dirent[] fsPromises.glob(pattern[, options]): AsyncIterator<string | fs.Dirent>
pattern may also be an array of patterns.
| Name | Description |
|---|---|
pattern |
A glob string, or an array of them. Matches from any pattern are included. |
options.cwd |
Directory the pattern is relative to. Default process.cwd(). |
options.exclude |
A function called with each candidate (a string, or a Dirent when withFileTypes is set). Return true to skip it, and a skipped directory is not entered. |
options.withFileTypes |
If true, return Dirent objects instead of path strings. Default false. |
callback |
Called as callback(err, matches). |
Pattern syntax: * matches any run of characters inside one path segment, ? matches one character, ** matches any number of directories (including none), [abc] and [a-z] match a set or range, and {a,b} matches either alternative.
Returns: matched paths relative to cwd as strings (or Dirent objects).
Errors: ERR_INVALID_ARG_TYPE for a bad pattern or option. Unreadable subdirectories are skipped rather than raising an error.
Usage notes
- Order is not defined. Sort the results if you need stable output, such as in tests.
- Results are relative to
cwd, not absolute. Join them withcwdbefore opening. - Dotfiles: a leading
*does not match names that start with a dot in most glob engines; check with a quick test before depending on it for hidden files. excludeprunes early. Excludingnode_modulesstops the walk from descending, which can be much faster than filtering results afterwards.- Prefer the async iterator for big trees.
globSync()and the callback form build the whole array in memory first. - Backslashes are escapes in patterns on every platform. Write patterns with
/, even on Windows. - Stability: available since Node.js 22 and stable in Node.js 24. In older releases it printed an experimental warning.
- Not a regular expression. For complex matching, list files with
fs.readdir()andrecursive: trueand filter with your own code.