THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This example shows multiple ways to get the current logged username of the running Operating System.
Each OS has a login profile to log into System.
Nodejs os
module provides current Operation System information.
userInfo() function returns an object containing OS information of a user and directory.
First, Import os
module into code using require() function.
Here is an example code
var os = require('os')
console.log(os.userInfo());
console.log(os.userInfo().username);//john
Output:
{
uid: -1,
gid: -1,
username: 'john',
homedir: 'C:\\Users\\john',
shell: null
}
john
Another way, using global process property
console.log(process.env.username)//john
you can get username with sevaral functions - os.userInfo() that returns object contains following information
os.userInfo()
: returns object contains username and home directory
os.userInfo().username
- returns current logged username
> os.hostname()
'john'
> os.userInfo()
{
uid: -1,
gid: -1,
username: 'john',
homedir: 'C:\\Users\\john',
shell: null
}
> os.userInfo().username
'john'
>
> process.env.username
'John'
🧮 Tags
Recent posts
Multiple ways to iterate a loop with index and element in array in swift How to reload a page/component in Angular? How to populate enum object data in a dropdown in angular| Angular material dropdown example How to get the current date and time in local and UTC in Rust example Angular 13 UpperCase pipe tutorial | How to Convert a String to Uppercase exampleRelated posts