Create directory with permission in node js

I am trying to create a folder using the mkdirp node module. but it is created with a resolution of 0775, but I need to create with a resolution of 0777. The official documentation says that it is 0777 by default, but in my case it is 0755. Can someone help me? code:

var new_location = 'public/images/u/7/'; mkdirp(new_location, function(err) { if (err) { } else { } }); 
+5
source share
2 answers

The documentation states that the default value is 0777 & (~process.umask()) , which means that your umask value is "subtracted" from 0777. Since umask is usually 002 or 022, you end up with 0775 or 0755.

However, even if you grant permission 0777 to mkdirp() , the underlying system call will still apply the umask value. To avoid this, you need to clear umask, create the directory with the permission you need, and (optionally) restore umask to its previous value:

 var oldmask = process.umask(0); mkdirp(new_location, '0777', function(err) { process.umask(oldmask); if (err) ... ... }); 

Alternatively, you can use fs.chmod() to set the correct permissions after creating the directory.

+9
source

try:

  var fs = require('fs'); var new_location = 'public/images/u/7/'; fs.mkdir(new_location , 0755, function (err) { if (err) {} }); } 

See also here https://nodejs.org/api/fs.html#fs_fs_mkdir_path_mode_callback

0
source

Source: https://habr.com/ru/post/1240340/


All Articles