Published at
Updated at
Reading time
2min
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 native "globbing" is included in Node core since v22.17.

The new utility comes in three flavors.

fsPromises.glob()

This fsPromise.glob version returns and an AsyncIterator which allows to end the globbing in a for await/of loop early if you have found what you're looking.

Alternatively, use Array.fromAsync if you don't care about breaking out early and want to discover all the files.

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

// Iterate over discovered file paths
for await (const entry of glob('**/*.txt')) {
  console.log(entry);
}

// Await all files paths
const files = await Array.fromAsync(glob('**/*.txt'));
console.log(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.1k 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