# How to check if a directory exists in Node.js

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Apr 10, 2020, updated Oct 1, 2022. Topics: Node.js.
Canonical URL: https://attacomsian.com/blog/nodejs-check-if-directory-exists

> A short guide to learn how to use the native File System Node.js module to check if a directory exists.

The Node.js `fs` native module provides several useful methods that you can use to work with directories. The simplest way to check if a certain directory exists in Node.js is by using the `fs.existsSync()` method.

The `existsSync()` method asynchronously checks for the existence of the given directory. Here is an example:

```javascript
const fs = require('fs')

// directory to check if exists
const dir = './uploads'

// check if directory exists
if (fs.existsSync(dir)) {
  console.log('Directory exists!')
} else {
  console.log('Directory not found.')
}
```

The `existsSync()` method returns `true` if the path exists, `false` otherwise.

If you prefer to use an **asynchronous check**, use the `fs.access()` method instead. This method takes a path as input and tests the user's permissions.

Let us look at the below example that uses `fs.access()` to check if the given directory exists:

```javascript
const fs = require('fs')

// directory to check if exists
const dir = './uploads'

// check if directory exists
fs.access(dir, err => {
  console.log(`Directory ${err ? 'does not exist' : 'exists'}`)
})
```

Read [this guide](https://attacomsian.com/blog/reading-writing-files-nodejs) to learn more about reading and writing files in a Node.js application.
