• Node.js Video Tutorials

Node.js - os.userInfo() Method



The Node.js os.userInfo() method of os module returns an object contains information about the current effective user like 'username', 'uid', 'gid', 'shell', and 'homedir'.

If there is no username or homedir for the user, a SystemError will be thrown. The value of the property named 'homedir' returned by the os.userInfo() method is provided by the operating system.

Following are the properties of the object returned object.

  • uid − This is a user identifier, which will specify the about the current effective user.

  • gid − This is a group ID; it is the identifier that links a user to other users who share certain things in common.

  • username − This specifies the username of the current effective user.

  • homedir − This specifies the home directory of the current effective user.

  • shell − This is a command line interpreter which is currently being used by the current effective user.

Syntax

Following is the syntax of the Node.js os.userInfo() method

os.userInfo([options])

Parameters

This method accepts only one parameter options which is an optional parameter.

  • encoding This specifies the character encoding for the returned data. If the encoding is set to 'buffer', then the values of username, shell, and homedir will be the instances of 'Buffer'. If the encoding is set to 'utf8' then values will be the instances of 'utf8'. By default, the instances are 'utf8'.

Return value

The returned object of this method contains the properties named 'username', 'uid', 'gid', 'shell', and 'homedir'. Whereas on Windows, the 'uid' and 'gid' are −1, and the 'shell' is null.

Example

When there are no parameters passed to the method, it will take the default value as 'utf8' and the values of the returned object properties will be 'Buffer' instances.

In the following example, we are trying to print the information of currently effective user by logging the Node.js userInfo() method to the console.

const os = require('os');

console.log(os.userInfo());

Output

{ 
   uid: 1000,
   gid: 1000,
   username: 'webmaster',homedir: '/home/webmaster',
   shell: '/bin/bash' 
}

Note − To get the accurate result, better execute the above code in local.

After executing the above program, the os.userInfo() method returns an object which contains information about the current effective user as shown in the output below.

{
   uid: -1,
   gid: -1,
   username: 'Lenovo',
   homedir: 'C:\\Users\\Lenovo',
   shell: null
}

Example

If the 'encoding' of the os.userInfo() method is set to 'utf8', the values of the returned object properties will be 'Buffer' instances.

In the following example, we are trying to specify the encoding value as 'utf8'. Then we are printing the information of currently effective user.

const os = require('os');

var option = {
    encoding: 'utf8'
};

console.log(os.userInfo(option));

Output

{ 
   uid: 1000,
   gid: 1000,
   username: 'webmaster',
   homedir: '/home/webmaster',
   shell: '/bin/bash' 
}

Note − To get the accurate result, better execute the above code in local.

After executing the above program, the returned object of the os.userInfo() method prints the values of 'username', 'uid', 'gid', 'shell', and 'homedir' as 'utf8' instances.

{
   uid: -1,
   gid: -1,
   username: 'Lenovo',
   homedir: 'C:\\Users\\Lenovo',
   shell: null
}

Example

In the following example, we are trying to specify the encoding value as 'buffer'. Then we are returning the information of currently effective user by logging the userInfo() method to the console.

const os = require('os');

var option = {
   encoding: 'buffer'
};

console.log(os.userInfo(option));

Output

{ 
   uid: 1000,
   gid: 1000,
   username: <Buffer 77 65 62 6d 61 73 74 65 72>,
   homedir: <Buffer 2f 68 6f 6d 65 2f 77 65 62 6d 61 73 74 65 72>,
   shell: <Buffer 2f 62 69 6e 2f 62 61 73 68> 
}

Note − To get the accurate result, better execute the above code in local.

After executing the above program, the returned object of the os.userInfo() method prints the values of 'username', 'uid', 'gid', 'shell', and 'homedir' as 'buffer' instances.

{
  uid: -1,
  gid: -1,
  username: <Buffer 4c 65 6e 6f 76 6f>,
  homedir: <Buffer 43 3a 5c 55 73 65 72 73 5c 4c 65 6e 6f 76 6f>,
  shell: null
}
nodejs_os_module.htm
Advertisements