os.userInfo([options])


  • options <Object>
    • encoding <string> 用于解释生成字符串的字符编码。 如果 encoding 设置为 'buffer'usernameshellhomedir 的值将是 Buffer 实例。默认值: 'utf8'
  • 返回:<Object>

返回有关当前有效用户的信息。在 POSIX 平台上,这通常是密码文件的一个子集。返回的对象包含 usernameuidgidshellhomedir。在 Windows 上,uidgid 字段为 -1,而 shellnull

【Returns information about the currently effective user. On POSIX platforms, this is typically a subset of the password file. The returned object includes the username, uid, gid, shell, and homedir. On Windows, the uid and gid fields are -1, and shell is null.】

os.userInfo() 返回的 homedir 值由操作系统提供。这与 os.homedir() 的结果不同,后者会先查询环境变量来获取主目录,然后才回退到操作系统的响应。

【The value of homedir returned by os.userInfo() is provided by the operating system. This differs from the result of os.homedir(), which queries environment variables for the home directory before falling back to the operating system response.】

如果用户没有 usernamehomedir,则抛出 SystemError

【Throws a SystemError if a user has no username or homedir.】