os.userInfo([options])


  • options <Object>

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

os.userInfo() 方法返回当前有效用户的信息。 在 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>

The os.userInfo() method 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 several 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.