Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How do I use chmod with Node.js

Tags: chmod mode nodejs

How do I use chmod with Node.js

Problem

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.

fs.chmod(path, mode, [callback])

Asynchronous chmod(2). No arguments other than a possible exception are given to the completion callback.

fs.chmodSync(path, mode)

Synchronous chmod(2).

(from the Node.js documentation)

If I do something like

fs.chmodSync('test', 0755);

nothing happens (the file isn't changed to that mode).

fs.chmodSync('test', '+x');

doesn't work either.

I'm working on a Windows machine btw.

Problem courtesy of: pvorb

Solution

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 exists on *nix machines.

Solution courtesy of: qiao

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How do I use chmod with Node.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×