Node.js fs.chmodSync() Method - GeeksforGeeks

08 Oct, 2021
The fs.chmodSync() method is used to synchronously 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, group or others.Syntax:  
fs.chmodSync( 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 string or octal integer constant that denotes the permission to be granted. The logical OR operator can be used to separate multiple permissions.
Below examples illustrate the fs.chmodSync() method in Node.js:Example 1: This example shows the usage of string constants and OR operator to give the file permissions. 
javascript
// Node.js program to demonstrate the// fs.chmodSync method  // Import the filesystem moduleconst fs = require('fs');  // Allowing only read permissionconsole.log("Giving only read permission to user");fs.chmodSync("example.txt", fs.constants.S_IRUSR);  // Check the file modeconsole.log("Current File Mode:",     fs.statSync("example.txt").mode);  // Reading the fileconsole.log("File Contents:",     fs.readFileSync("example.txt", 'utf8'));  // Trying to write to filetry {  console.log("Trying to write to file");  fs.writeFileSync('example.txt', "Hello");}catch (e) {  console.log("Error Found, Code:", e.code);}  // Allowing both read and write permissionconsole.log("\nGiving both read and write"    + " permissions to user");  fs.chmodSync("example.txt",    fs.constants.S_IRUSR | fs.constants.S_IWUSR);  // Check the file modeconsole.log("Current File Mode:",    fs.statSync("example.txt").mode);  console.log("Trying to write to file");fs.writeFileSync('example.txt', "World");  console.log("File Contents:",    fs.readFileSync("example.txt", 'utf8'));
Output:  
Giving only read permission to user
Current File Mode: 33024
File Contents: Hello
Trying to write to file
Error Found, Code: EACCES

Giving both read and write permissions to user
Current File Mode: 33152
Trying to write to file
File Contents: World
Example 2: This example shows the usage of octal integer constants to give the file permissions. 
javascript
// Node.js program to demonstrate the// fs.chmodSync method  // Import the filesystem moduleconst fs = require('fs');  // Allowing only read permissionconsole.log("Giving only read permission to everyone");fs.chmodSync("example.txt", 0o444);  // Check the file modeconsole.log("Current File Mode:",     fs.statSync("example.txt").mode);  // Reading the fileconsole.log("File Contents:",     fs.readFileSync("example.txt", 'utf8'));  // Trying to write to filetry {  console.log("Trying to write to file");  fs.writeFileSync('example.txt', "Hello");}catch (e) {  console.log("Error Found, Code:", e.code);}  // Allowing both read and write permissionconsole.log("\nGiving both read and "     + "write permission to everyone");fs.chmodSync("example.txt", 0o666);  // Check the file modeconsole.log("Current File Mode:",     fs.statSync("example.txt").mode);  console.log("Trying to write to file");fs.writeFileSync('example.txt', "World");  console.log("File Contents:",     fs.readFileSync("example.txt", 'utf8'));
Output:  
Giving only read permission to everyone
Current File Mode: 33060
File Contents: Hello
Trying to write to file
Error Found, Code: EACCES

Giving both read and write permission to everyone
Current File Mode: 33206
Trying to write to file
File Contents: World
Reference: https://nodejs.org/api/fs.html#fs_fs_chmodsync_path_mode 
nidhi_biet
akshaysingh98088
Node.js-fs-module
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Express.js express.Router() Function
Mongoose Populate() Method
Express.js res.json() Function
Express.js res.render() Function
Mongoose | findByIdAndUpdate() Function
Express.js express.Router() Function
How to set input type date in dd-mm-yyyy format using HTML ?
Differences between Functional Components and Class Components in React
How to create footer to stay at the bottom of a Web page?
How to float three div side by side using CSS?