Search code examples
node.jspermissionsfs

How to change all file and folder permissions in the same folder using node.js


I want remove all read only attributes after my project is built.

I tried fs.chmodSync('path',0o741)

It works when the path is a file, but doesn’t work when the path is a folder. Is there something like chmod -R in linux that changes all the permissions in the folder in node.js?


Solution

  • You can install the chmodr module.

    var chmodr = require('chmodr');
    
    chmodr('/folder', 0o777, (err) => {
      if (err) {
        console.log('Failed to execute chmod', err);
      } else {
        console.log('Success');
      }
    });
    

    Alternatively you can look at its implementation, it's a small 100 line file, it calls fs.chmod/fs.chmodSync recursively on all children of a directory.