Skip to content

fs.existsSync returns wrong value on windows if there are no permissions on the given file #17921

Description

  • Version: v9.2.0
  • Platform: windows 8.1
  • Subsystem: fs

Steps to reproduce the issue

  1. Create a file (for example 1.txt)
  2. Disable permissions inheritance and remove all inherited permissions from the file
  3. Execute fs.existsSync('1.txt')

What is the expected behavior

The file exists (and it is also listed if you readdirSync it's parent directory), so the return value should be true.

What went wrong

The return value is false, like the file doesn't exist. If it is working as intended, then consider this issue as a feature request. However, if you're not aware of this behavior, then I'm pretty sure it is a bug.

Activity

  1. added
    fsIssues and PRs related to file-system APIs and the fs module.
    windowsIssues and PRs related to the Windows platform.
    on Dec 30, 2017
  2. jaulz commented on Jan 17, 2018

    @jaulz

    I have a similar issue where existsSync returns true though the file does not exist. Happens on Windows 10 with NodeJS v8.9.1.

  3. Johnsavadkuhi commented on Jan 17, 2018

    @Johnsavadkuhi
    Contributor

    let me to see your code

  4. added
    docIssues and PRs related to Node.js documentation.
    on Jan 17, 2018
  5. bnoordhuis commented on Jan 17, 2018

    @bnoordhuis
    Member

    Perhaps the documentation could be improved (I'll add the label) but fs.exists() works the same way as fs.stat() in this regard: it queries the operating system for file metadata for the specified path and passes on the error that the operating system returns.

    The difference is that fs.stat() would have returned an error whereas fs.exists() returns false because true or false is the only thing it's allowed to return.

    You might get better results with fs.access(), depending on the situation.

  6. jaulz commented on Jan 17, 2018

    @jaulz

    Following the code I use:

    console.log('bin')
    console.log(bin)
    console.log('fs.existsSync(bin)')
    console.log(fs.existsSync(bin))
    try {
      console.log('fs.accessSync(bin)')
      console.log(fs.accessSync(bin))
    } catch (error) {
      console.log('-')
    }
    while (true) { }

    This is the output:

    bin
    C:\Users\XXX\.appcelerator\install\7.0.1\package\bin\appc
    fs.existsSync(bin)
    true
    fs.accessSync(bin)
    undefined
    

    I put a while(true) {} to stop at that very stage but if I run >cd C:\Users\XXX\.appcelerator in parallel the output is: The system cannot find the path specified.

    Any idea?

  7. bnoordhuis commented on Jan 17, 2018

    @bnoordhuis
    Member

    @jaulz That's probably some ACL or permissions issue. The directory is there, it's just inaccessible.

  8. jaulz commented on Jan 18, 2018

    @jaulz

    @bnoordhuis not sure... I cannot even find that folder via command nor via explorer.

  9. 6-8-axnw1bom81v5xa3nh48c commented on Jan 20, 2018

    @6-8-axnw1bom81v5xa3nh48c

    I am the reporter of this issue, but I accidentally deleted my account.

    @bnoordhuis So, this actually works as intended? Ok, then it would be a good idea to mention it somewhere in the doc. Or, even better, change the behavior of fs.existsSync to try to read parent dir in case of error and if the file is located in the parent dir, then return true, no matter if the actual file is accessible or not.

    @Johnsavadkuhi What does your thumbs down mean: you cannot reproduce the issue, or you disagree that this issue should be considered as a bug? "Let me to see your code" - my code or @jaulz's code?

  10. bzoz commented on Feb 1, 2018

    @bzoz
    Contributor

    In this case existsSync call to stat fails with EPERM, maybe we could make existsSync return false when the error is ENOENT? Or, is there a reason why existsSync does not use accessSync?

  11. bnoordhuis commented on Feb 1, 2018

    @bnoordhuis
    Member

    In this case existsSync call to stat fails with EPERM, maybe we could make existsSync return false when the error is ENOENT?

    fs.existsSync() always returns false when the stat fails.

    Or, is there a reason why existsSync does not use accessSync?

    Yes, see #4679 - Windows CI issue.

  12. added a commit that references this issue on Mar 28, 2018
  13. added a commit that references this issue on May 8, 2018
  14. cawoodm commented on Mar 27, 2019

    @cawoodm

    I'm using NodeJS v11.10.0 on Windows 10 and having the same issue: existsSync says a file does not exist but it does and I do have permissions on it. What to do?

  15. BridgeAR commented on Mar 27, 2019

    @BridgeAR
    Member

    @cawoodm you probably want to either use fs.accessSync or you directly want to call fs.open as recommended in the documentation.

    There is no guarantee that the file does not exist if fs.existsSync returns false. It only let's you know if the file is there and is accessible for you. There could be multiple reasons for the cause in case it returns false.

  16. cawoodm commented on Mar 30, 2019

    @cawoodm

    Oops, I think my problem was with backslashes in the path C:\temp being interpreted as tabs. Sorry for jostling!

  17. ohuu commented on May 4, 2020

    @ohuu

    Sorry to comment on a closed issue but I'm seeing the same thing.

    OS: Windows 10
    Node version: 13.12.0

    The file in question is java installed via scoop C:\Users\Oliver\scoop\apps\openjdk11\current\bin\java. It has permissions and definitely exists but existsSync returns false!

    Calling accessSync returns this error:

    Error: ENOENT: no such file or directory, access 'C:/Users/Oliver/scoop/apps/openjdk11/current/bin/java'
        at Object.accessSync (fs.js:208:3) {
      errno: -4058,
      syscall: 'access',
      code: 'ENOENT',
      path: 'C:/Users/Oliver/scoop/apps/openjdk11/current/bin/java'
    }
    

    calling open returns undefined.

  18. ohuu commented on May 7, 2020

    @ohuu

    Finally got to the bottom of this. Windows has to have the extension of the file in order to check that it exists so fs.existsSync('C:/Users/Oliver/scoop/apps/openjdk11/current/bin/java.exe') works but fs.existsSync('C:/Users/Oliver/scoop/apps/openjdk11/current/bin/java') doesn't

  19. badrelmers commented on Mar 23, 2022

    @badrelmers

    this is really a stupid language

  20. added a commit that references this issue on Jul 27, 2026
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.windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions