Repository navigation
fs.readdir recursive: is parentPath supposed to be the same as path? #51955
Description
Activity
- addeddocIssues and PRs related to Node.js documentation.Issues and PRs related to Node.js documentation.
on Mar 4, 2024 - addedfsIssues and PRs related to file-system APIs and the fs module.Issues and PRs related to file-system APIs and the fs module.
on Mar 4, 2024 This is correct,
dirent.pathanddirent.parentPathhave the same value.The only difference is that
dirent.pathis doc-deprecated with DEP0178. Users should use.parentPathin new projects and migrate from.pathin old ones.
It's likely to become runtime-deprecated in the upcoming major release.parentPathproperty has value of parent directory of its dirent, relative1 to the current working directory.
E.g. for/a/b/c/d/e/f/gfile, if our current working directory is/a/band we doreaddir('../b/c/d', ...)dirent for that file will look like{ name: 'g', parentPath: '../b/c/d/e/f'}.Historically,
pathproperty had path to the file itself and it got changed, which is why it's in process of being renamed to less ambiguousparentPath.IMHO in direct comparison
parentPathis preferable overrelativePath, because likewise we can getrelativePathvalue with justpath.join(dirent.parentPath, dirent.name)as well asabsolutePathwithpath.resolve(dirent.parentPath, dirent.name). However, internallyreaddir()deals withparentPathand by providing it directly we can skip doingpath.join()for every dirent internally andpath.dirname()in userspace.Thus said, the issue of
.pathproperty 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
-
in case if we passed absolute path to
readdir,parentPathwill also be absolute ↩
Reacted by Alexander Willer-
Closing as answered, let us know if you have more questions.
- I guess my only feedback is that it’s confusing that a new word is being coined for dirname. I had no idea what parentPath was supposed to mean, and I’ve published dozens of path libraries over the years. Why not just use dirname, for consistency with path.dirname()? Or dir, for consistency with path.parse() (which should have used dirname IMHO)?It’s ok, I’m not really asking you to justify your decisions or debate me, etc. I’m just throwing these things out for consideration, since I know I won’t be the only one thinking these things… tbh these are the types of inconsistencies that make people want to pull their hair out with Node. Sent from my iPhoneOn Mar 13, 2024, at 5:31 PM, Antoine du Hamel ***@***.***> wrote: Closing as answered, let us know if you have more questions. —Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>Reacted by mb-realpage and Ju YuwolReacted by mb-realpage
Is it node-specific? Pretty sure the underlying issues with naming is inconsistent naming between OSes in the first place. I use
folderin place ofdirectorybecause that's how windows file explorer calls them. The entirety offsmodule and its unix lingo was pretty painful to learn about because of it. AndparentPathisn'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.- addedquestionIssues asking questions about Node.js.Issues asking questions about Node.js.
on Jun 25, 2024 Reopening with
questionlabel, as it seems this has become a question about the namingReacted by mb-realpageThe way I see it is that
parentPathandpathare not always aliases in that they have the same values. Example:const files = await readdir(path, { withFileTypes: true });
fileshere will have an array ofDirEnt. this example, I would have aDirEntwithparentPathof the parent directory of the file whereaspathis the full path of the file. This is the issue of calling them aliases, this is simply not always true.To get the same
pathvalue, 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.
github-actions commented
on May 23, 2026 on May 23, 2026 – with GitHub ActionsContributorMore actionsThis 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.- addedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on May 23, 2026 github-actions commented
on Jun 22, 2026 on Jun 22, 2026 – with GitHub ActionsContributorMore actionsThis 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.
Affected URL(s)
No response
Description of the problem
I'm curious what the purpose of
parentPathis on Dirent objects when usingfs.readdirorfs.readdirSyncwhen bothwithFileTypesandrecursiveare set to true.The reason for this issue is that I haven't found a situation where
parentPathdiffers frompath, 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
relativepath versusparentPath, sinceparentPathis just thepath.dirname(path)of each path, isn't it? If not, perhapsparentPathisn't the best name? Maybe it should bebaseorrootor something?