Open In App

Node.js fs.filehandle.chmod() Method

Last Updated : 28 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The fs.filehandle.chmod() method is an inbuilt application programming interface of class fs.filehandle within File System module which is used to change the permission of the particular file.

Syntax: 

const filehandle.chmod( mode )

Parameter: This method accepts single parameter mode that holds the integer value.
Return Value: This method returns a pending promise which contain no value in it.

Below programs illustrates the use of fs.filehandle.chmod() method.

Example 1: Filename: index.js  

Javascript




// Node.js program to demonstrate the
// fs.filehandle.chmod() method
const fs = require('fs');
const fsPromises = fs.promises;
 
console.log("content of file before "
        + "operation :- " +
        (fs.readFileSync('example.txt')));
 
// Initiating asynchronise function
async function funct() {
 
    // Initializing filehandle
    let filehandle = null;
 
    try {
 
        // Creating and initiating filehandle
        filehandle = await fsPromises
                .open('example.txt', 'r+');
 
        // Modifying the file permission
        // by using chmod() method
        const prom = filehandle.chmod(1);
 
    } finally {
 
        if (filehandle) {
 
            // Close the file if it is opened.
            console.log("permission is changed"
                        + " to read only");
 
            console.log("content of file before"
                    + " operation :- " +
                (fs.readFileSync('example.txt')));
 
            await filehandle.close();
        }
    }
}
 
funct().catch(console.error);


Directory structure before running the program: 

Directory structure after running the program: 

Run index.js file using the following command: 

node index.js

Output: 

content of file before operation :- Content of example.txt file 
permission is changed to read only 
content of file after operation :- Content of example.txt file 
 

Example 2: Filename: index.js  

Javascript




// Node.js program to demonstrate the
// fs.filehandle.chmod() method
const fs = require('fs');
const fsPromises = fs.promises;
 
// data for the new file
let data = "This is a file containing"
        + " a collection of books.";
 
// Name of the file to be created
let file = "books.txt";
 
// Creating the new file 'books.txt'
fs.writeFile(file, data, (err) => {
 
    // Catching error
    if (err) {
        console.log(err);
    }
});
 
// Using fs.exists() method
fs.exists(file, (exists) => {
    if (exists) {
        console.log("content of file "
            + "before operation :- " +
            (fs.readFileSync(file)));
    }
});
 
// Initiating asynchronise function
async function funct() {
 
    // Initializing filehandle
    let filehandle = null;
 
    try {
 
        // Creating and initiating  filehandle
        filehandle = await fsPromises.open(file, 'r+');
 
        // Modifying the file permission
        // by using chmod() method
        const prom = filehandle.chmod(1);
 
    } finally {
 
        if (filehandle) {
 
            // Close the file if it is opened.
            console.log("permission is "
                + "changed to read only");
                 
            console.log("content of file"
                + "after operation :- " +
                (fs.readFileSync(file)));
 
            await filehandle.close();
        }
    }
}
 
funct().catch(console.error);


Directory structure before running the program: 

Directory structure after running the program: 
 

Run index.js file using the following command: 

node index.js

Output: 

content of file before operation :- This is a file containing a collection of books. 
permission is changed to read only 
content of file after operation :- This is a file containing a collection of books. 
 

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_filehandle_chmod_mode 



Previous Article
Next Article

Similar Reads

Node.js filehandle.writeFile() Method from class: FileHandle
The filehandle.writeFile() method is used to define in the File System module of Node.js. The File System module basically interacts with the hard disk of the user’s computer. The fs.writeFile() method asynchronously writes data to a file, replacing the file if it already exists. Syntax: filehandle.writeFile(data, options) Parameter: This method ac
3 min read
Node.js filehandle.stat() Method from class: FileHandle
The filehandle.stat() method is defined in File System module of Node.js. The File System module is basically to interact with the hard disk of the user's computers. The filehandle.stat() method gives some information specific to files and folders using methods defines on stats objects(object returned by stat method). The method returns a resolved
3 min read
Node.js fs.chmod() Method
The fs.chmod() 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: The Windows platform only supports the changing of the write permission. It also does not support the distinction between the permissions of user, g
3 min read
Node.js fsPromises.chmod() Method
The fsPromises.chmod() 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: The Windows platform only supports the changing of the write permission. It does not support the distinction between the permissions of the
3 min read
Node.js filehandle.readFile() Method
The filehandle.readFile() method is used to asynchronously read the file contents. This method reads the entire file into the buffer. It Asynchronously reads the entire contents of a file. Syntax: filehandle.readFile( options ) Parameters: The method accepts single parameter as mentioned above and described below: options: It holds the encoding of
2 min read
Node.js filehandle.appendFile() Method
The filehandle.appendFile() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the user’s computer. The appendFile() method is used to asynchronously append new data in the existing file or if the file does not exist then the file is created first and after that given data i
3 min read
Node.js filehandle.truncate() Method
The filehandle.truncate() method is defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the user’s computer. The truncate() method used to modify the inner contents of the file by ‘len’ bytes. If len is shorter than the file’s current length, the file is truncated to that length of len
3 min read
Node.js filehandle.close() Method
The filehandle.close() method is used to asynchronously close the given file descriptor thereby clearing the file that is associated with it. This will allow the file descriptor to be reused for other files. Calling filehandle.close() method on a file descriptor while some other operation is being performed on it may lead to undefined behavior. Syn
1 min read
Node.js filehandle.read() Method
The filehandle.read() method reads the file using a file descriptor. In order to read files without file descriptors the readFile() method of the filehandle package can be used. Node.js is used for server-side scripting. Reading and writing files are the two most important operations that are performed in any application. Node.js offers a wide rang
2 min read
Node.js fs.filehandle.datasync() Method
The fs.filehandle.datasync() method is an inbuilt application programming interface of class fs.filehandle within File System module which is used to sync the data of the file. Syntax: const filehandle.datasync() Parameter: This method does not accept any parameter.Return value: This method returns an pending promise which contain no value in it. B
3 min read