node.jsfilesystems

How do I check if a file is executable in node.js?


How do I check if a file is executable in node.js?

Maybe something like

fs.isExecutable(function (isExecutable) {

})

Solution

  • You would use the fs.stat call for that.

    The fs.stat call returns a fs.Stats object.

    In that object is a mode attribute. The mode will tell you if the file is executable.

    In my case, I created a file and did a chmod 755 test_file and then ran it through the following code:

    var fs = require('fs');
    test = fs.statSync('test_file');
    console.log(test);
    

    What I got for test.mode was 33261.

    This link is helpful for converting mode back to unix file permissions equivalent.