Skip to content

fs.readdir recursive: is parentPath supposed to be the same as path? #51955

Description

@jonschlinkert

Affected URL(s)

No response

Description of the problem

I'm curious what the purpose of parentPath is on Dirent objects when using fs.readdir or fs.readdirSync when both withFileTypes and recursive are set to true.

The reason for this issue is that I haven't found a situation where parentPath differs from path, it's always the same value. My hunch is that it's not supposed to be and there is probably a bug.

FWIW, I think it would be more useful to include the relative path versus parentPath, since parentPath is just the path.dirname(path) of each path, isn't it? If not, perhaps parentPath isn't the best name? Maybe it should be base or root or something?

Activity

  1. added
    docIssues and PRs related to Node.js documentation.
    on Mar 4, 2024
  2. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Mar 4, 2024
  3. LiviaMedeiros commented on Mar 4, 2024

    @LiviaMedeiros
    Member

    This is correct, dirent.path and dirent.parentPath have the same value.

    The only difference is that dirent.path is doc-deprecated with DEP0178. Users should use .parentPath in new projects and migrate from .path in old ones.
    It's likely to become runtime-deprecated in the upcoming major release.

    parentPath property has value of parent directory of its dirent, relative1 to the current working directory.
    E.g. for /a/b/c/d/e/f/g file, if our current working directory is /a/b and we do readdir('../b/c/d', ...) dirent for that file will look like { name: 'g', parentPath: '../b/c/d/e/f'}.

    Historically, path property had path to the file itself and it got changed, which is why it's in process of being renamed to less ambiguous parentPath.

    IMHO in direct comparison parentPath is preferable over relativePath, because likewise we can get relativePath value with just path.join(dirent.parentPath, dirent.name) as well as absolutePath with path.resolve(dirent.parentPath, dirent.name). However, internally readdir() deals with parentPath and by providing it directly we can skip doing path.join() for every dirent internally and path.dirname() in userspace.

    Thus said, the issue of .path property with same value is planned to be fixed. :)
    If you have suggestions on adding more useful properties (e.g. absolutePath) that are worth the performance cost of doing it internally, or maybe methods (e.g. dirent.resolve()), feel free to share the usecases.

    Footnotes

    1. in case if we passed absolute path to readdir, parentPath will also be absolute ↩

  4. aduh95 commented on Mar 13, 2024

    @aduh95
    Contributor

    Closing as answered, let us know if you have more questions.

  5. jonschlinkert commented on Mar 13, 2024

    @jonschlinkert
    Author
  6. GabenGar commented on Jun 25, 2024

    @GabenGar

    Is it node-specific? Pretty sure the underlying issues with naming is inconsistent naming between OSes in the first place. I use folder in place of directory because that's how windows file explorer calls them. The entirety of fs module and its unix lingo was pretty painful to learn about because of it. And parentPath isn't technically a wrong term either, because it refers to the syntactic part of the path string (which may or may not be a directory when being opened), not the file system definition of directory.

  7. avivkeller commented on Jun 25, 2024

    @avivkeller
    Member

    Reopening with question label, as it seems this has become a question about the naming

  8. mitchellsimoens commented on Sep 11, 2024

    @mitchellsimoens

    The way I see it is that parentPath and path are not always aliases in that they have the same values. Example:

    const files = await readdir(path, { withFileTypes: true });

    files here will have an array of DirEnt. this example, I would have a DirEnt with parentPath of the parent directory of the file whereas path is the full path of the file. This is the issue of calling them aliases, this is simply not always true.

    To get the same path value, you need to do:

    for (const file of files) {
      const filePath = path.join(file.parentPath, file.name);
    }

    Not optimal but if there are different OS reasons or whatever, not the end of the world either. However, the documentation does not lead you down this path either.

  9. github-actions commented on May 23, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  10. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on May 23, 2026
  11. github-actions commented on Jun 22, 2026

    @github-actions
    Contributor

    This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 240 days).
    If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.

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

    docIssues and PRs related to Node.js documentation.fsIssues and PRs related to file-system APIs and the fs module.questionIssues asking questions about Node.js.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions