Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs.existsSync does not seem to work synchronously #52953

Closed
cfish68 opened this issue May 12, 2024 · 1 comment
Closed

fs.existsSync does not seem to work synchronously #52953

cfish68 opened this issue May 12, 2024 · 1 comment

Comments

@cfish68
Copy link

cfish68 commented May 12, 2024

Version

v18.19.1, v20.10.0

Platform

Linux Inspiron-5493 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

const fs = require('fs');

if (!fs.existsSync('./new')){
fs.mkdir('./new', (err) => {
if (err) throw err;
console.log('Directory created');
})
}

if (fs.existsSync('./new')){
fs.rmdir('./new', (err) => {
if (err) throw err;
console.log('directory removed');
})
}else{
console.log('Directory does not exist');
}

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

When running from terminal the unexpected output is produced.

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

The expected behavior is, given the directory 'new' does not exist, the program should produce and then immediately delete the directory 'new'. If it does exist then it should just delete it.
This is the expected behavior because existsSync is expected to work synchronously .

What do you see instead?

It either creates the directory or deletes it, but not both.

Additional information

I checked this out on 2 versions both on a linux and a windows machine and I got the same result. When using a vsc debugger it did indeed give me the expected output.

@marco-ippolito
Copy link
Member

marco-ippolito commented May 12, 2024

You are mixing sync functions and async callback based function.
Use mkdirSync and rmdirSync and it will work as expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants