fs.accessSync(path[, mode])
同步测试用户对指定 path 的文件或目录的权限。mode 参数是一个可选的整数,用于指定要执行的可访问性检查。mode 应该是值 fs.constants.F_OK,或者是由 fs.constants.R_OK、fs.constants.W_OK 和 fs.constants.X_OK 通过按位或组成的掩码(例如 fs.constants.W_OK | fs.constants.R_OK)。检查 文件访问常量 以获取 mode 的可能值。
【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. mode should be either the value
fs.constants.F_OK or a mask consisting of the bitwise OR of any of
fs.constants.R_OK, fs.constants.W_OK, and fs.constants.X_OK (e.g.
fs.constants.W_OK | fs.constants.R_OK). Check File access constants for
possible values of mode.】
如果任何辅助功能检查失败,将抛出 Error。否则,该方法将返回 undefined。
【If any of the accessibility checks fail, an Error will be thrown. Otherwise,
the method will return undefined.】
import { accessSync, constants } from 'node:fs';
try {
accessSync('etc/passwd', constants.R_OK | constants.W_OK);
console.log('can read/write');
} catch (err) {
console.error('no access!');
}