Skip to content

readdirSync cannot read files properly when using { recursive: true, withFileTypes: true } #51773

Closed as not planned

Description

@prfd

Version

v18.17.1

Platform

No response

Subsystem

fs

What steps will reproduce the bug?

mkdir -p test/include
touch test/libvips-cpp.so.42
const fs = require('fs');
fs.readdirSync('test', {recursive: true, withFileTypes: true});

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior? Why is that the expected behavior?

await fs.promises.readdir('test', {recursive: true, withFileTypes: true})
[
  Dirent { name: 'include', path: 'folder', [Symbol(type)]: 2 },
  Dirent {
    name: 'libvips-cpp.so.42',
    path: 'folder',
    [Symbol(type)]: 1
  }
]

What do you see instead?

[
  Dirent { name: 'include', path: 'folder', [Symbol(type)]: 2 },
  Dirent {
    name: 'libvips-cpp.so.42',
    path: 'folder',
    [Symbol(type)]: 1
  },
  Dirent {
    name: undefined,
    path: 'folder/include',
    [Symbol(type)]: undefined
  },
  Dirent {
    name: undefined,
    path: 'folder/include',
    [Symbol(type)]: undefined
  }
]

Additional information

No response

Activity

  1. richardlau commented on Feb 15, 2024

    @richardlau
    Member

    Can reproduce in 18.17.1 but not in 18.19.0:

    $ nvm run 18.17.1
    Running node v18.17.1 (npm v9.6.7)
    Welcome to Node.js v18.17.1.
    Type ".help" for more information.
    > const fs = require('fs');
    undefined
    > fs.readdirSync('test', {recursive: true, withFileTypes: true});
    [
      Dirent { name: 'include', path: 'test', [Symbol(type)]: 2 },
      Dirent { name: 'libvips-cpp.so.42', path: 'test', [Symbol(type)]: 1 },
      Dirent {
        name: undefined,
        path: 'test/include',
        [Symbol(type)]: undefined
      },
      Dirent {
        name: undefined,
        path: 'test/include',
        [Symbol(type)]: undefined
      }
    ]
    >
    $ nvm run 18.19.0
    Running node v18.19.0 (npm v10.2.3)
    Welcome to Node.js v18.19.0.
    Type ".help" for more information.
    > const fs = require('fs');
    undefined
    > fs.readdirSync('test', {recursive: true, withFileTypes: true});
    [
      Dirent { name: 'include', path: 'test', [Symbol(type)]: 2 },
      Dirent { name: 'libvips-cpp.so.42', path: 'test', [Symbol(type)]: 1 }
    ]
    >
  2. richardlau commented on Feb 15, 2024

    @richardlau
    Member

    Probably related to #48858
    #48698 was included in Node.js 18.19.0.

  3. prfd commented on Feb 15, 2024

    @prfd
    Author

    Probably related to #48858 #48698 was included in Node.js 18.19.0.

    So, all stable versions of electron have a broken readdirSync, that's unfortunate.

    https://releases.electronjs.org/

  4. richardlau commented on Feb 15, 2024

    @richardlau
    Member

    Probably related to #48858 #48698 was included in Node.js 18.19.0.

    So, all stable versions of electron have a broken readdirSync, that's unfortunate.

    https://releases.electronjs.org/

    You'll need to discuss that with Electron as we don't control what versions of Node.js they use.

  5. ankit142 commented on Feb 15, 2024

    @ankit142

    Try the async approach:

    const fs = require('fs').promises;
    
    async function listDirectoryRecursive(directory) {
        try {
            const dirents = await fs.readdir(directory, { withFileTypes: true });
            for (const dirent of dirents) {
                console.log(dirent);
                if (dirent.isDirectory()) {
                    await listDirectoryRecursive(`${directory}/${dirent.name}`);
                }
            }
        } catch (error) {
            console.error('Error reading directory:', error);
        }
    }
    
    listDirectoryRecursive('test');
    
    
  6. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Apr 20, 2024
  7. 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.

  8. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on May 23, 2026
  9. 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

    fsIssues and PRs related to file-system APIs and the fs module.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