Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readNode

Match files with fs.glob

Find test files by pattern using Node 22’s built-in glob and globSync functions.

You want a list of every test file in a project, for a small script that runs them or counts them. You reach for a glob package, which adds a dependency to a script that is twenty lines long. Node 22 can do it for you: glob and globSync were added to fs in 22.0.0.

The API was experimental in Node 22.0.0.

The demo project has src/index.ts, src/components/Button.tsx, src/components/Button.test.tsx, src/lib/format.ts and src/lib/format.test.ts. It also has a stray test file at node_modules/pkg/index.test.ts. The files are empty, because only the names matter.

Loop over the matches

glob from node:fs/promises takes a pattern and gives back an async iterator, so you read it with for await. The braces in the pattern match either extension. Save this as list-tests.mjs and run node list-tests.mjs.

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

const files = []
for await (const file of glob('**/*.test.{ts,tsx}')) {
  files.push(file)
}
console.log(files.sort())
// [
//   'node_modules/pkg/index.test.ts',
//   'src/components/Button.test.tsx',
//   'src/lib/format.test.ts'
// ]

You get three paths, relative to the folder where you ran the command. I sort the array because I do not rely on the order of the results.

The first path is the problem: the pattern also matched a file inside node_modules.

Skip node_modules

Pass an exclude function to leave out matches or prune directories. Check the final path segment to skip nested node_modules directories too.

import { glob } from 'node:fs/promises'
import { basename } from 'node:path'

const files = []
for await (const file of glob('**/*.test.{ts,tsx}', {
  exclude: (path) => basename(path) === 'node_modules',
})) {
  files.push(file)
}
console.log(files.sort())
// [ 'src/components/Button.test.tsx', 'src/lib/format.test.ts' ]

Only the two real test files are left, and the scan never lists anything under node_modules.

Use the sync version

In a short script you may not want the loop. globSync comes from node:fs and returns an array. The cwd option sets the folder the pattern runs from, and the paths come back relative to it.

import { globSync } from 'node:fs'

console.log(globSync('*.ts', { cwd: 'src/lib' }).sort())
// [ 'format.test.ts', 'format.ts' ]

Both files in src/lib match, and neither path has a src/lib/ prefix. I use the async form when the script already awaits other work, and the sync form for a build step that runs once.

There is also a callback form, fs.glob(pattern, options, callback), for code that already uses callbacks. The fs documentation lists all three forms and the options.

Update, June 2025. Node 22.17.0 marked glob stable. Starting in Node 22.14.0, exclude also accepts an array of glob patterns.