How to use the chmodSync function from fs

Find comprehensive JavaScript fs.chmodSync code examples handpicked from public code repositorys.

The fs.chmodSync method is used to change the file mode (permissions) of a file or directory synchronously in Node.js.

178
179
180
181
182
183
184
185
186
187
 * add execute permission on file
 * @param  {string} filePath - file path
 * @return {undefined}
 */
exports.addExecPermission = function (filePath) {
    fs.chmodSync(filePath, '755');
}
/**
 * copy file, will make dir first if src is not exist
 * @param  {string}   src    - original file
fork icon124
star icon815
watch icon42

+ 30 other calls in file

11
12
13
14
15
16
17
18
19
20
exports.access = co.promisify(fs.access);
exports.accessSync = fs.accessSync;
exports.appendFile = co.promisify(fs.appendFile);
exports.appendFileSync = fs.appendFileSync;
exports.chmod = co.promisify(fs.chmod);
exports.chmodSync = fs.chmodSync;
exports.chown = co.promisify(fs.chown);
exports.chownSync = fs.chownSync;
exports.close = co.promisify(fs.close);
exports.closeSync = fs.closeSync;
fork icon22
star icon45
watch icon26

How does fs.chmodSync work?

The fs.chmodSync() method is a synchronous Node.js file system method that changes the permissions of a file or directory on the file system with a specified mode. The fs.chmodSync() method takes two arguments: The first argument is the file path, which is the location of the file or directory to change the permissions. The second argument is the mode to apply to the file or directory. The mode can be specified as an octal number, or as a string representing an octal number. The fs.chmodSync() method will throw an error if it fails to change the permissions of the file or directory.

85
86
87
88
89
90
91
92
93
94
  assert.ok((fs.statSync(file1).mode & 0o777) & mode_async);
} else {
  assert.strictEqual(mode_async, fs.statSync(file1).mode & 0o777);
}

fs.chmodSync(file1, mode_sync);
if (common.isWindows) {
  assert.ok((fs.statSync(file1).mode & 0o777) & mode_sync);
} else {
  assert.strictEqual(mode_sync, fs.statSync(file1).mode & 0o777);
fork icon16
star icon65
watch icon0

432
433
434
435
436
437
438
439
440
441
442
443
  };


  fs.chmod(nonexistentFile, 0o666, common.mustCall(validateError));


  assert.throws(
    () => fs.chmodSync(nonexistentFile, 0o666),
    validateError
  );
}

fork icon42
star icon19
watch icon0

Ai Example

1
2
3
4
const fs = require("fs");

// Set permissions of the file to read, write and execute for the owner
fs.chmodSync("/path/to/file.txt", 0o700);

In this example, the fs.chmodSync method is used to change the permissions of the file located at /path/to/file.txt to rwx------ (read, write, and execute for the owner). The second argument passed to the method, 0o700, is an octal value representing the permission mode.

145
146
147
148
149
150
151
152
153
154
fs.writeFileSync(p, fs.readFileSync(process.execPath))
if (modifyBinary)
  modifyBinary(p)
fs.appendFileSync(p, fs.readFileSync(a))
appendMeta(p, a)
fs.chmodSync(p, 0o755)
const result = require('child_process').spawnSync(p)
if (result.status !== null) {
  // Will be left for debugging if failed to run.
  fs.unlinkSync(a)
fork icon14
star icon126
watch icon5

83
84
85
86
87
88
89
90
91
92
// Copy jvm
// projectDir.copy('resources/linux/java-jre', readyAppDir.path('java-jre'));
// Copy preinst
// var postinst = projectDir.read('resources/linux/DEBIAN/postinst');
// packDir.write('DEBIAN/postinst', postinst);
// fs.chmodSync(packDir.path('DEBIAN/postinst'), '0755');

// Build the package...
childProcess.exec('fakeroot dpkg-deb -Zxz --build ' + packDir.path().replace(/\s/g, '\\ ') + ' ' + debPath.replace(/\s/g, '\\ '),
    function (error, stdout, stderr) {
fork icon3
star icon7
watch icon4

698
699
700
701
702
703
704
705
706
707
if (entry.type === 'Directory') {
  const needChmod = !this.noChmod &&
    entry.mode &&
    (st.mode & 0o7777) !== entry.mode
  const [er] = needChmod ? callSync(() => {
    fs.chmodSync(entry.absolute, entry.mode)
  }) : []
  return this[MAKEFS](er, entry)
}
// not a dir entry, have to remove it
fork icon3
star icon2
watch icon0

2111
2112
2113
2114
2115
2116
2117
2118
2119

// mode: r/w 33188
// mode: r/w/x 33261

if (execute_chk.checked) {
    chmod = fs.chmodSync(filename, '755');
} else {
    chmod = fs.chmodSync(filename, '33188');
}
fork icon2
star icon5
watch icon1

1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
 */
const attachServerWithConfig = (server, onSuccess) => {
  if (process.env.SOCKET || process.env.PORT && isNaN(+process.env.PORT)) {
    server.listen(process.env.SOCKET || process.env.PORT, () => {
      if (onSuccess) {
        fs.chmodSync(server.address(), 0o666);
        onSuccess(server.address());
      }
    });
  } else {
fork icon2
star icon4
watch icon2

53
54
55
56
57
58
59
60
61
    }

    // console.debug(dir);
    if (!fs.existsSync(dir)) {
      fs.mkdirSync(dir);
      fs.chmodSync(dir, permission);
    }
  });
};
fork icon0
star icon0
watch icon1

10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
  fs.unlinkSync(file);
} catch (e) {
  // Try to override file permission
  /* istanbul ignore next */
  if (e.code === 'EPERM') {
    fs.chmodSync(file, '0666');
    fs.unlinkSync(file);
  } else {
    throw e;
  }
fork icon0
star icon0
watch icon1

35
36
37
38
39
40
41
42
43
44

{
  const file = path.join(tmpdir.path, `chmodSync-${suffix}.txt`);
  fs.writeFileSync(file, 'test', 'utf-8');

  fs.chmodSync(file, input);
  assert.strictEqual(fs.statSync(file).mode & 0o777, mode);
}

{
fork icon0
star icon0
watch icon0

+ 4 other calls in file

145
146
147
148
149
150
151
152
153
154
155
    message: 'The "path" argument must be of type string or an instance ' +
             'of Buffer or URL.' +
             common.invalidArgTypeHelper(input)
  };
  assert.throws(() => fs.chmod(input, 1, common.mustNotCall()), errObj);
  assert.throws(() => fs.chmodSync(input, 1), errObj);
});


process.on('exit', function() {
  assert.strictEqual(openCount, 0);
fork icon0
star icon0
watch icon0

+ 9 other calls in file