Node.js includes a native glob utility
- Published at
- Updated at
- Reading time
- 1min
The two most popular glob utilities (minimatch and glob) are responsible for over 500 million weekly npm downloads. Something so common should be included in the Node core library, right?
Today, I discovered that they are and that Node supports native "globbing" since v22.17.
The utility comes in three flavors.
I don't know why fsPromises
needed to return an async iterator but to access the files you can use the newish Array
method.
import { glob } from 'node:fs/promises';
const files = await glob('**/*.txt');
console.log(await Array.fromAsync(files));
fs
is callback-based if that's your jam.
import { glob } from 'node:fs';
await glob('**/*.txt', (error, files) => {
console.log(files);
});
And, of course, there's a synchronous version with fs
, too.
import { globSync } from 'node:fs';
console.log(globSync('**/*.txt'));
Now, I'm sure there are some minor differences to the popular libraries, but I bet it'll be fine for some quick file-grepping in JavaScript.
Join 6.2k readers and learn something new every week with Web Weekly.