Node.js | fs.accessSync() Method

The fs.accessSync() method is used to synchronously 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.

Syntax:

fs.accessSync( path, mode )

Parameters: This method accepts two parameters as mentioned above and described below:

  • path: It is a String, Buffer or URL which 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.

Below examples illustrate the fs.accessSync() method in Node.js:

Example 1: This example shows the testing of the read and write permission of a file.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.accessSync() method
  
// Import the filesystem module
const fs = require('fs');
  
// Allowing only read permission
console.log("Giving only read permission to user");
fs.chmodSync("example.txt", fs.constants.S_IRUSR);
  
// Test the read permission
console.log('\n> Checking Permission for reading the file');
try {
  fs.accessSync('example.txt', fs.constants.R_OK);
  console.log('File can be read');
} catch (err) {
  console.error('No Read access');
}
  
// Test both the read and write permission
console.log('\n> Checking Permission for reading and writing to file');
try {
  fs.accessSync('example.txt', fs.constants.R_OK | fs.constants.W_OK);
  console.log('File can be read and written');
} catch (err) {
  console.error('No Read and Write access');
}

chevron_right


Output:

Giving only read permission to 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.accessSync() method
  
// Import the filesystem module
const fs = require('fs');
  
// Test the if the file exists
console.log('\n> Checking if the file exists');
try {
  fs.accessSync('example.txt', fs.constants.F_OK);
  console.log('File does exist');
} catch (err) {
  console.error('File does not exist');
}
  
console.log('\nCreating the file');
fs.writeFileSync("example.txt", "Test File");
  
// Test the if the file exists again
console.log('\n> Checking if the file exists');
try {
  fs.accessSync('example.txt', fs.constants.F_OK);
  console.log('File does exist');
} catch (err) {
  console.error('File does not exist');
}

chevron_right


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_accesssync_path_mode




My Personal Notes arrow_drop_up

Im listening

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet

Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.