In a Node.js application, you can use the fs.access() method provided to check whether the given 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 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 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 Windows operating system and will only check for the existence of the file in the current directly.

If you are interested in checking the existence of a file only, Node.js provides 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'}`);
});

Take a look at this guide to learn more about checking for the existence of a directory (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 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!`);
}

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

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Subscribe to RSS Feed.

👋 If you enjoy reading my articles and want to support me to continue creating free tutorials, Buy me a coffee (cost $5) .

Need help to launch a new product? I am available for contract work. Hire me to accomplish your business goals with engineering and design.