Skip to content

fs.glob add option to only include files (not directories) in the results entries #52752

Description

@theoludwig

What is the problem this feature will solve?

Since Node.js v22, with this PR: #51912, it is possible to use fs.promises.glob for matching file paths based on specified patterns.

However, the results of entries also includes directories, but other famous userland library (e.g: globby) only returns files (not directories).

Example

With a file structure like the following:

$ mkdir -p foo/bar && touch foo/bar.md
$ tree foo
foo
├── bar
└── bar.md

2 directories, 1 file

And the following code:

import fs from "node:fs"
import { globby } from "globby"

console.log("fs.glob", await Array.fromAsync(fs.promises.glob("foo/**")))
console.log("globby", await globby("foo/**"))

It prints:

fs.glob [ 'foo', 'foo/bar.md', 'foo/bar' ]
globby [ 'foo/bar.md' ]

What is the feature you are proposing to solve the problem?

Add 2 options to fs.glob:

Both default to false, to keep same behavior, so no breaking changes is introduced.

Options based on the fast-glob library which globby uses under the hood.

What alternatives have you considered?

export const globby = async (patterns) => {
  const files = []
  for await (const entry of fs.promises.glob(patterns)) {
    const stats = await fs.promises.stat(entry)
    if (stats.isFile()) {
      files.push(entry)
    }
  }
  return files
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.fsIssues and PRs related to file-system APIs and the fs module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions