The fs.access() method is used to test the permissions of a given file or directory. The permissions to be checked can be specified as a parameter using file access constants. It is also possible to check multiple file permissions by using the bitwise OR operator to create a mask with more than one file constant.
Note: It is not recommended to use the fs.access() method to check for the accessibility of a file before calling fs.open(), fs.readFile() or fs.writeFile(), because it introduces a race condition since the file state may be changed by other processes after the test.
Syntax:
fs.access( path, mode, callback )
Parameters: This method accepts three parameters as mentioned above and described below:
- path: It is a String, Buffer or URL that denotes the path of the file or directory for which the permission has to be tested.
- mode: It is an integer value that denotes the permission to be tested for. The logical OR operator can be used to separate multiple permission. It can have the values fs.constants.F_OK, fs.constants.R_OK, fs.constants.W_OK and fs.constants.X_OK. It is an optional parameter. The default value is fs.constants.F_OK.
- callback: It is a function that would be called when the method is executed.
- err: It is an error that would be thrown if the method fails.
Below examples illustrate the fs.access() method in Node.js:
Example 1: This example shows the testing of the read and write permission of a file.
javascript
const fs = require('fs');
console.log("Giving only read permission to the user");
fs.chmodSync("example_file.txt", fs.constants.S_IRUSR);
fs.access('example_file.txt', fs.constants.R_OK, (err) => {
console.log('\n> Checking Permission for reading the file');
if (err)
console.error('No Read access');
else
console.log('File can be read');
});
fs.access('example_file.txt', fs.constants.R_OK
| fs.constants.W_OK, (err) => {
console.log('\n> Checking Permission for reading"
+ " and writing to file');
if (err)
console.error('No Read and Write access');
else
console.log('File can be read and written');
});
|
Output:
Giving only read permission to the user
> Checking Permission for reading the file
File can be read
> Checking Permission for reading and writing to file
No Read and Write access
Example 2: This example shows the testing of a file if it exists.
javascript
const fs = require('fs');
fs.access('example_file.txt', fs.constants.F_OK, (err) => {
console.log('\n> Checking if the file exists');
if (err) {
console.error('File does not exist');
console.log('\nCreating the file');
fs.writeFileSync("example_file2.txt", "Test File");
fs.access('example_file2.txt', fs.constants.F_OK, (err) => {
console.log('\n> Checking if the file exists');
if (err)
console.error('File does not exist');
else {
console.log('File does exist');
}
});
}
else {
console.log('File does exist');
}
});
|
Output:
> Checking if the file exists
File does not exist
Creating the file
> Checking if the file exists
File does exist
Reference: https://nodejs.org/api/fs.html#fs_fs_access_path_mode_callback
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
27 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...