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, group, or others.
fs.chmod( path, mode, callback )
Parameters: This method accepts three 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.
- callback: It is a function that would be called when the method is executed.
- err: It is an error that would be thrown if the method fails.
Below examples illustrate the fs.chmod() method in Node.js:
Example 1: This example shows the usage of octal integer constants to give the file permissions.
Granting only read access to user Reading the file contents This is an example text file. Trying to write to file Error Code: EACCES Granting read and write access to user Trying to write to file Reading the file contents This file has now been edited.
Example 2: This example shows the usage of string constants and OR operator to give the file permissions.
Granting only read access to user File Contents: This file now has been edited. Trying to write to file Error Occurred, Error Code: EACCES Granting both read and write permission to user Trying to write to file File Contents: This file now has been edited.
- Nodejs | Automatic restart NodeJs server with nodemon
- Nodejs console.timeLog() Method
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
- Nodejs | Web Crawling using Cheerio
- Web-Socket in NodeJS
- Signup Form Using Nodejs and MongoDB
- Dockerizing a simple Nodejs app
- Nodejs | DNS
- Encrypting Data in NodeJS
- Difference between NodeJS and AngularJS
- Nodejs | Jimp
- Nodejs – Connect Mysql with Node app
- Nodejs | DNS | setServers()
- NodeJS | Building simple REST API in express
- NodeJs - Handling invalid routes
- Nodejs | authentication using Passportjs and passport-local-mongoose
- Nodejs | GM charcoal() Function
- Nodejs | GM blur() Function
- Nodejs | GM sharpen() Function
Improved By : nidhi_biet