os.userInfo([options])


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

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

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

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

  • options <Object>
    • encoding <string> Character encoding used to interpret resulting strings. If encoding is set to 'buffer', the username, shell, and homedir values will be Buffer instances. Default: 'utf8'.
  • Returns: <Object>

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.

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.

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