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

> Learn how to check if a file exists in Node.js using fs.existsSync() for a synchronous check, or fs.access() for an asynchronous, non-blocking approach.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2018-09-26 | Topics: [Node.js](https://flaviocopes.com/tags/node/) | Canonical: https://flaviocopes.com/how-to-check-if-file-exists-node/

The way to check if a file exists in the filesystem, using [Node.js](https://flaviocopes.com/nodejs/), is by using the `fs.existsSync()` method:

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

const path = './file.txt'

try {
  if (fs.existsSync(path)) {
    //file exists
  }
} catch(err) {
  console.error(err)
}
```

This method is synchronous. This means that it's blocking. To check if a file exists in an asynchronous way, you can use `fs.access()`, which checks the existence of a file without opening it:

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

const path = './file.txt'

fs.access(path, fs.F_OK, (err) => {
  if (err) {
    console.error(err)
    return
  }

  //file exists
})
```
