Check whether a folder already exists with Bun

495 views Asked by At

How do I check whether a folder already exists using Bun?

I tried the following without success:

const path = "folderName"
const file = Bun.file(path)
await file.exists()

Based on the guide for working with files: https://bun.sh/guides/read-file/exists

I know it's for checking whether a file exists, but usually you can do the same thing for folders. I checked that the path is correct by referring for a file inside the folder folderName/fileName, and it worked.

How do I check whether a folder already exists with Bun?

1

There are 1 answers

0
Simon G On

To test if a file/folder exist , you can use :

const fExist = path => !! Array.from(
      new Bun.Glob(path).scanSync({ onlyFiles: false })
    )[0]

If you want to know if it's a folder and not a file, you can compare with the scanSync without option