![]() |
VOOZH | about |
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:
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.
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.
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