Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get OS username in NodeJS?

How would I get/find the username that the computer owner is using currently (while logged in), using NodeJS?

I have searched around a bit, but haven't found anything.

like image 778
drowZ Avatar asked Nov 04 '16 13:11

drowZ


People also ask

How do I find my node JS username?

To get the OS current user id or ( uid ) in Node. js, you can use the userInfo() method from the os module and then use the uid property from the object returned.

How do I find my Windows browser Username?

you can do it w/ very small java applet that has to signed you just need System. getProperty("user.name") The solution will work on any browser supporting java.

How do I get node JS terminal?

js/JavaScript code. To launch the REPL (Node shell), open command prompt (in Windows) or terminal (in Mac or UNIX/Linux) and type node as shown below. It will change the prompt to > in Windows and MAC. You can now test pretty much any Node.

What is process ENV user?

process. env is the process's environment variables, which are supplied by the OS to the process. This object can really contain just about anything, as specified the OS and the process that launches it, but by default Windows stores the username in USERNAME and Unix-like systems (Linux, macOS, etc.) store it in USER .


2 Answers

I am not sure why, but someone added an answer and then deleted it quickly after... I was fast enough to catch it though, and after checking, it is the shortest and most effective way of doing what I asked before:

require("os").userInfo().username

The only problem is, in Windows 10, it returns the first name of the owner account that has been used (just a heads up). Everything else works completely fine!

like image 61
drowZ Avatar answered Sep 18 '22 20:09

drowZ


This one object you will get username:

let os = require('os') console.log(os.userInfo()); 
like image 31
ankur kumar Avatar answered Sep 19 '22 20:09

ankur kumar