57

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.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
drowZ
  • 1,783
  • 2
  • 12
  • 17

8 Answers8

109

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!

Martlark
  • 14,208
  • 13
  • 83
  • 99
drowZ
  • 1,783
  • 2
  • 12
  • 17
24

This one object you will get username:

let os = require('os')
console.log(os.userInfo());
Frankenmint
  • 1,570
  • 3
  • 18
  • 33
ankur kumar
  • 419
  • 2
  • 9
15

process.env.USER Should work also at least in MAC or Linux. e.g.

let user = process.env.USER || ""
Abu Shumon
  • 1,834
  • 22
  • 36
3

Definitely, the easiest way to do it is using username

Install:

$ npm install username

Then:

const username = require('username');

(async () => {
    console.log(await username());
    //=> 'current_username'
})();
Abraham
  • 8,525
  • 5
  • 47
  • 53
  • Hai @carlos, I have implemented the same and tried to add this code in one GET rest api. I am trying to attach the response of username() function to response.send() method but its not returning. Please find the code i am trying below: var user = (async () => { await username(); })(); res.status(200).send({ success: 'true', message: user }); – Kishore Konangi Nov 06 '18 at 05:10
  • Please ingnore my above comment, it got resolved . Hai @carlos, When i tried to move the same code to linux server and using GET rest api i am hitting from client machine. I am getting the linux server user but not the client user who is hitting the url. The same worked in my windows machine as i am running the application locally. Any help regarding this.!? – Kishore Konangi Nov 06 '18 at 05:40
  • 1
    @kishoreaoe this module will basically return to you the logged in user(in this case the server user), you will need to work around that to find out how does this module can help you get the client user, probably not because it will get you the root or logged in username. – Abraham Nov 08 '18 at 05:47
  • 2
    Not a good habit to just use a random 3rd party module for everything. – Ian Smith Jan 24 '22 at 06:51
2

Dependency free method:

function getUsername() {
    return (
        process.env.SUDO_USER ||
        process.env.C9_USER ||
        process.env.LOGNAME ||
        process.env.USER ||
        process.env.LNAME ||
        process.env.USERNAME
    );
}
John Doherty
  • 3,669
  • 36
  • 38
0

you can simply use

app.getPath('home')

It'll return the home path of the current user.

Harsh Makwana
  • 237
  • 3
  • 13
  • I use this for electron on Ubuntu and it works fine. Unsure why the downvotes. `/home/` `const home = app.getPath('home');` – beeeliu Nov 03 '22 at 22:33
  • Reasons why people might downvote this include: (1) assumes electron [though the OP admittedly did include the electron tag] & (2) this does not return the user information but rather the user's home directory, which might be related but is not the same. Node's built-in [`os.userInfo`](https://nodejs.org/api/os.html#osuserinfooptions) and [`os.homedir`](https://nodejs.org/api/os.html#oshomedir) are likely more suitable for what the OP actually asks. – jacobq Mar 09 '23 at 23:19
-1

If it doesn't need to be cross operating systems (just *nix based), one way you could do (keep in mind that exec could be potentially risky to use if you parameterize it):

const Promise = require( 'bluebird' ),
      exec    = Promise.promisify( require( 'child_process' ).exec );

exec( 'id -un' ).then( ( username )=> {
 // do something about it
});

If you want to use bluebird for promises, don't forget about: npm install bluebird --save

Miroslav Saracevic
  • 1,446
  • 1
  • 13
  • 32
  • Checkout the [`userid` package](https://www.npmjs.com/package/userid) (which I maintain). Does this without spawning extra processes. – Cameron Tacklind Aug 27 '21 at 21:07
-8

If you want to get information about the client witch call a route on your server, you have to parse client useragent.

https://msdn.microsoft.com/en-us/library/ms537503(v=vs.85).aspx

You can get client user agent with node using those examples :

How to handle user-agent in nodejs environment?

Community
  • 1
  • 1
Kevin Grosgojat
  • 1,386
  • 8
  • 13