How do I use chmod with Node.js

前端 未结 2 1530
面向向阳花
面向向阳花 2020-12-09 14:34

How do I use chmod with Node.js?

There is a method in the package fs, which should do this, but I don\'t know what it takes as the second argument.

相关标签:
2条回答
  • 2020-12-09 15:15

    The correct way to specify Octal is as follows:

    fs.chmodSync('test', 0o755); 
    

    Refer to the file modes here.

    0 讨论(0)
  • 2020-12-09 15:21

    According to its sourcecode /lib/fs.js on line 508:

    fs.chmodSync = function(path, mode) {
      return binding.chmod(pathModule._makeLong(path), modeNum(mode));
    };
    

    and line 203:

    function modeNum(m, def) {
      switch (typeof m) {
        case 'number': return m;
        case 'string': return parseInt(m, 8);
        default:
          if (def) {
            return modeNum(def);
          } else {
            return undefined;
          }
      }
    }
    

    it takes either an octal number or a string.

    e.g.

    fs.chmodSync('test', 0755);
    fs.chmodSync('test', '755');
    

    It doesn't work in your case because the file modes only exist on *nix machines.

    0 讨论(0)
提交回复
热议问题