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.
The userInfo()
function returns an object containing the OS information of a user and directory.
First, Import theos
module into the 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 the global process property
console.log(process.env.username)//john
you can get a username with several functions - os.userInfo()
that returns an object containing the following information
os.userInfo()
: returns an object containing 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
global property> process.env.username
'John'
🧮 Tags
Recent posts
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts