Node.js fsPromises.lchmod() Method
Last Updated :
08 Oct, 2021
Improve
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.
// 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);
});
node index.js
Output:
File permission changed to read only! EPERMReference: https://nodejs.org/api/fs.html#fs_fspromises_lchmod_path_mode