Open In App

Node.js fsPromises.lchmod() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The fsPromises.lchmod() method is used to change the permissions of a given path. These permissions can be specified using string constants or octal numbers that correspond to their respective file modes.

Note: This method is only implemented on macOS. It changes the permissions of a file then resolves the Promise with no arguments upon success.

Syntax:

fsPromises.lchmod( path, mode)

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

  • path: It is a string, Buffer or URL that denotes the path of the file of which the permission has to be changed.
  • mode: It is an octal integer constant that denotes the permission to be granted. The logical OR operator can be used to separate multiple permissions.

Example: This example illustrates the fsPromises.lchmod() method in Node.js:
Create an “example.txt” file for granting the permission to read to the user.

Filename: index.js




// Node.js program to demonstrate the 
// fsPromises.lchmod method 
    
// Import the filesystem module 
const fs = require('fs'); 
const fsPromises = fs.promises;
  
// Changing file permission to read only
fsPromises.lchmod('example.txt', 0o400)
.then(function() {
  console.log("File permission changed to read only!");
  
  try {
      fs.writeFileSync('x.txt','Hello World');
  }
  catch (e) {
    console.log(e.code);
  }
})
.catch(function(error) {
  console.log(error);
});


Step to run this program: Run index.js file using the following command:

node index.js

Output:

File permission changed to read only!
EPERM

Reference: https://nodejs.org/api/fs.html#fs_fspromises_lchmod_path_mode


Last Updated : 08 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads