There are 2 ways to check if a directory exists with Node.js: Synchronously and Asynchronously. It is useful when we want to confirm whether a folder is already available or not before creating it.
Check if a directory exists in Node.js using the following methods:
- Call
fs.existsSync()
method from thefs
module to confirm the existence of a directory synchronously. - On the other hand,
fs.access()
method checks for a directory asynchronously in a Node.js project.
Let's see some examples using both methods.
Method 1: Synchronously Check If a Directory Exists with fs.existsSync()
It is the simplest way to test whether a path exists or not in the filesystem. The fs.existsSync()
is a synchronous method. That's why we can directly use it inside an if
statement.
It accepts a path as its argument. If that path is present in the filesystem, it will return true
or false
otherwise.
const { existsSync } = require('fs')
const checkDirectory = (path) => {
if (existsSync(path)) {
console.log(`Directory ${path} exists`)
} else {
console.log(`Directory ${path} doesn't exist`)
}
}
checkDirectory('./images')
I have a checkDirectory()
function in this example that takes the directory path. Here I am checking if the images
directory is present or not.
When I pass that path to the existsSync()
method, it will return true
if the images
directory exists. Then the if
block will execute.
If the directory doesn't exist, we can create a new directory using Node.js inside the else block.
Method 2: Asynchronously Check If a Directory Exists with fs.access()
If you want to test a directory path asynchronously, you can use the fs.access()
method from the Node.js fs
module. Since fs.access()
method is asynchronous, it returns a promise.
The main difference between synchronous and asynchronous methods is that a synchronous method blocks the main thread. Therefore, JavaScript stops executing other code until this method completes.
On the other hand, an asynchronous method runs without blocking the main thread. So, JavaScript can handle other requests at the same time. That's why it is recommended to use asynchronous methods as much as possible.
const { access } = require('fs/promises')
const checkDirectory = async (path) => {
try {
await access(path)
console.log(`Directory ${path} exists`)
} catch (error) {
console.log(`Directory ${path} doesn't exist`)
}
}
checkDirectory('./images')
To handle promise, I am using async...await
syntax in this example. If the images folder exists, the access()
method will not return anything. But if the folder doesn't exist, it will throw an error.
That's why I am wapping this method with try...catch
to handle any error. When we get the error, it means the given directory path is not available. You can now create a new directory in this case.
Also Read: How to Get All Files in Directories (Recursively) with Node.js
Conclusion
We have seen 2 methods fs.existsSync()
and fs.access()
to test a directory path in this article. Here, the fs.existsSync()
is a synchronous method, and fs.access()
is an asynchronous method.
This is the main difference between them. Now, it's up to you which method you want to use to check if a directory exists in your Node.js application.