- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
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 }