Node: `fs.existsSync` returns wrong value on windows if there are no permissions on the given file

Created on 30 Dec 2017  路  14Comments  路  Source: nodejs/node

  • 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.

doc fs windows

Most helpful comment

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

All 14 comments

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

let me to see your code

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.

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:

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?

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

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

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?

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?

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.

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?

@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.

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

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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danialkhansari picture danialkhansari  路  3Comments

stevenvachon picture stevenvachon  路  3Comments

cong88 picture cong88  路  3Comments

fanjunzhi picture fanjunzhi  路  3Comments

danielstaleiny picture danielstaleiny  路  3Comments