Outputting the username in Electron

Viewed 4732

I would really know how to output the username in Electron applications. So something like that:

const os = require('os')
const username = // the code or the username so that it can be displayed
document.write("The username is: " + username)

Is that possible?

3 Answers

Out of the box, the username is available through:

const os = require ('os');
const username = os.userInfo ().username;

Also (at least on Mac OS X and Linux) it can be obtained through the LOGNAME or USER environment variables:

username = process.env["LOGNAME"];
// or
username = process.env["USER"];

You can use the username node module ---

const username = require('username');

username().then(username => {
    console.log(username);
    //=> 'sindresorhus' 
});

sync() method returns the same but without promises

document.write(username.sync());
Related