Skip to content

Latest commit

 

History

History
71 lines (49 loc) · 1.83 KB

File metadata and controls

71 lines (49 loc) · 1.83 KB
description Modern alternatives to the glob package for file pattern matching and globbing

Replacements for glob

tinyglobby

tinyglobby provides a similar API.

Example:

import { glob } from 'glob' // [!code --]
import { glob } from 'tinyglobby' // [!code ++]

const files = await glob('**/*.ts')

Most options available to glob are available in tinyglobby, read more at the tinyglobby documentation.

fs.glob (native, since Node 22.x)

fs.glob is built into modern versions of Node.

Example:

import { glob } from 'glob' // [!code --]
import { glob } from 'node:fs/promises' // [!code ++]

const files = await glob('src/**/*.ts', { // [!code --]
const files = await Array.fromAsync(glob('src/**/*.ts', { // [!code ++]
  cwd,
}) // [!code --]
})) // [!code ++]

You can also iterate over the results asynchronously:

for await (const result of glob('src/**/*.ts', { cwd })) {
  // result is an individual path
  console.log(result)
}

Note

Node's built-in glob is more minimal and does not support negation patterns or fine-grained options like setting a max depth out of the box.

fdir

fdir offers similar functionality but through a different API (and tinyglobby is actually built on top of it).

Example:

import { fdir } from 'fdir' // [!code ++]
import { glob } from 'glob' // [!code --]

const files = new fdir() // [!code ++]
  .withBasePath() // [!code ++]
  .glob('src/**/*.ts') // [!code ++]
  .crawl(cwd) // [!code ++]
  .withPromise() // [!code ++]
const files = await glob('src/**/*.ts', { cwd, maxDepth: 6 }) // [!code --]