Repository navigation
fs.existsSync returns wrong value on windows if there are no permissions on the given file #17921
Description
Activity
- addedfsIssues and PRs related to file-system APIs and the fs module.Issues and PRs related to file-system APIs and the fs module.windowsIssues and PRs related to the Windows platform.Issues and PRs related to the Windows platform.
on Dec 30, 2017 I have a similar issue where
existsSyncreturns true though the file does not exist. Happens on Windows 10 with NodeJS v8.9.1.Reacted by Galaktikos, Gábor Kriskó, A, 1vomarek1, Michael Sorensen, Mark Hansen, jorugit, Taehyun Hwang, claudiogg88, IonDev and 1 moreReacted by John Savadkuhilet me to see your code
Reacted by Galaktikos, Amine Abdelli and Michal Jarnot- addeddocIssues and PRs related to Node.js documentation.Issues and PRs related to Node.js documentation.
on Jan 17, 2018 Perhaps the documentation could be improved (I'll add the label) but
fs.exists()works the same way asfs.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 whereasfs.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:
bin C:\Users\XXX\.appcelerator\install\7.0.1\package\bin\appc fs.existsSync(bin) true fs.accessSync(bin) undefinedI put a
while(true) {}to stop at that very stage but if I run>cd C:\Users\XXX\.appceleratorin parallel the output is:The system cannot find the path specified.Any idea?
Reacted by Marat Ahmetzyanov@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.
6-8-axnw1bom81v5xa3nh48c commented
on Jan 20, 2018 More actionsI 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.existsSyncto try to read parent dir in case of error and if the file is located in the parent dir, then returntrue, 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
existsSynccall tostatfails withEPERM, maybe we could makeexistsSyncreturn false when the error isENOENT? Or, is there a reason whyexistsSyncdoes not useaccessSync?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.
- added 2 commits that reference this issue
on Apr 4, 2018 - added a commit that references this issue
on May 8, 2018 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?
Reacted by Glen Thomas and Surya@cawoodm you probably want to either use
fs.accessSyncor you directly want to callfs.openas recommended in the documentation.There is no guarantee that the file does not exist if
fs.existsSyncreturnsfalse. 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 returnsfalse.Oops, I think my problem was with backslashes in the path
C:\tempbeing 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.0The file in question is
javainstalled via scoopC:\Users\Oliver\scoop\apps\openjdk11\current\bin\java. It has permissions and definitely exists butexistsSyncreturns false!Calling
accessSyncreturns 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
openreturnsundefined.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 butfs.existsSync('C:/Users/Oliver/scoop/apps/openjdk11/current/bin/java')doesn'tReacted by Justin M. KeyesReacted by solartes, Guillermo Cortés, SuppliedOrange, IonDev and Ricardo RomeroReacted by Andrewthis is really a stupid language
Reacted by Colin Ihrig, wishty, selene466, Jens A. Koch, Axel H., Matthieu VANCAYZEELE, black-ray, lechuhuuha, Ricardo Romero and Bohdan- added a commit that references this issue
on Jul 27, 2026
Steps to reproduce the issue
1.txt)fs.existsSync('1.txt')What is the expected behavior
The file exists (and it is also listed if you
readdirSyncit's parent directory), so the return value should betrue.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.