Getting nodejs file permissions from fs.stat mode
When you need to get file stats using NodeJS (which calls the unix stat
command in the background), you can use the fs.stat
call as shown below:
fs.stat('path/to/file', function (err, stats) { });
The stats
object returned here is an instance of fs.Stats
which contains a mode
property. You can use this property to determine the unix file permissions for the file path provided. The only problem is that this mode
property just gives you a number (as referenced in this GitHub issue). To view the permissions in the standard unix octal format (e.g. 0445, 0777 etc) you can use the following code:
var unixFilePermissions = '0' + (stats.mode & parseInt('777', 8)).toString(8);
Some examples of the mode
before and after calling the above snippet:
33188 -> 0644
33261 -> 0755