Published at
Updated at
Reading time
1min
This post is part of my Today I learned series in which I share all my web development learnings.

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.

fsPromises.glob()

I don't know why fsPromises.glob() needed to return an async iterator but to access the files you can use the newish Array.fromAsync method.

import { glob } from 'node:fs/promises';

const files = await glob('**/*.txt');
console.log(await Array.fromAsync(files));

fs.glob()

fs.glob() is callback-based if that's your jam.

import { glob } from 'node:fs';

await glob('**/*.txt', (error, files) => {
  console.log(files);
});

fs.globSync()

And, of course, there's a synchronous version with fs.globSync(), 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.

If you enjoyed this article...

Join 6.2k readers and learn something new every week with Web Weekly.

Web Weekly — Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles