fs.accessSync(path[, mode])


同步地测试用户对 path 指定的文件或目录的权限。 mode 参数是可选的整数,指定要执行的可访问性检查。 查看文件访问的常量以获取可能的 mode 值。 可以创建由两个或多个值的按位或组成的掩码(例如 fs.constants.W_OK | fs.constants.R_OK)。

如果任何可访问性检查失败,将抛出 Error。 否则,该方法将返回 undefined

try {
  fs.accessSync('etc/passwd', fs.constants.R_OK | fs.constants.W_OK);
  console.log('can read/write');
} catch (err) {
  console.error('no access!');
}

Synchronously tests a user's permissions for the file or directory specified by path. The mode argument is an optional integer that specifies the accessibility checks to be performed. Check File access constants for possible values of mode. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. fs.constants.W_OK | fs.constants.R_OK).

If any of the accessibility checks fail, an Error will be thrown. Otherwise, the method will return undefined.

try {
  fs.accessSync('etc/passwd', fs.constants.R_OK | fs.constants.W_OK);
  console.log('can read/write');
} catch (err) {
  console.error('no access!');
}