In a Node.js application, the fs.access() method can be used to check whether a file is readable, writable, or executable by the calling process. This method asynchronously tests a user's permissions for the file or directory.

The fs.access() method accepts up to three arguments: the path to the file or directory, the mode, and the callback function. The mode is an optional integer value that specifies the accessibility checks to be performed. You can find all possible mode values in the File Access Constants list.

Let us start with a basic example that checks if the given file is readable:

const fs = require('fs')

// file or directory path
const file = 'user.json'

// check if file is readable
fs.access(file, fs.constants.R_OK, err => {
  console.log(`${file} ${err ? 'is not readable' : 'is readable'}`)
})

The fs.constants.R_OK flag indicates that the file or directory is readable by the calling process.

Similarly, to check if the given file is writable by the calling process, you can use the fs.constants.W_OK flag as shown below:

// check if the file is writable
fs.access(file, fs.constants.W_OK, err => {
  console.log(`${file} ${err ? 'is not writable' : 'is writable'}`)
})

To check if the file is executable by the current process, just use the fs.constants.X_OK flag like below:

// check if file is executable
fs.access(file, fs.constants.X_OK, err => {
  console.log(`${file} ${err ? 'is not executable' : 'is executable'}`)
})

Note: The fs.constants.X_OK flag doesn't work for the Windows operating system and will only check for the file's existence in the current directory.

If you are interested in checking the existence of a file only, Node.js provides the fs.constants.F_OK constant for this purpose. This flag indicates that the file is visible to the calling process:

const fs = require('fs')

// file or directory path
const file = 'user.json'

// check if file exists in the current directory
fs.access(file, fs.constants.F_OK, err => {
  console.log(`${file} ${err ? 'does not exist' : 'exists'}`)
})

Read this guide to learn more about checking for the existence of a directory (which holds true for files too) in a Node.js application.

You can also combine multiple constants together to create a new accessibility condition. For example, the following example checks if the given file exists in the current directory and is writable by the calling process:

// check if the file exists in the current directory and is writable
fs.access(file, fs.constants.F_OK | fs.constants.W_OK, err => {
  if (err) {
    console.error(`${file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`)
  } else {
    console.log(`${file} exists, and is writable`)
  }
})

The fs module also provides a synchronous variant of the access() method called fs.accessSync() that synchronously performs the accessibility check for the given file or directory.

Here is an example that demonstrates how to use the fs.accessSync() method to check if the given file is both readable and writable by the current process:

// check if the file is readable and writable
try {
  fs.accessSync(file, fs.constants.R_OK | fs.constants.W_OK)
  console.log(`${file} is both readable and writable`)
} catch (err) {
  console.error(`${file} is not accessible!`)
}

Read this guide to learn more about reading and writing files in a Node.js application.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.