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

In Node the fs.stat method returns an fs.Stats object, you can get the file permission through the fs.Stats.mode property. From this post: Nodejs File Permissions


Another option that relies only on the built-in fs module is to use either fs.access or fs.accessSync. This method is easier than getting and parsing the file mode. An example:

const fs = require('fs');

fs.access('./foobar.sh', fs.constants.X_OK, (err) => {
    console.log(err ? 'cannot execute' : 'can execute');
});

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.