Bootstrap FreeKB - Node.js - Return username using os.userInfo().username (whoami)
Node.js - Return username using os.userInfo().username (whoami)

Updated:   |  Node.js articles

os.userInfo().username can be used to get the username of the user running your Node.js app.

If your package.json file does not have "type": "module" this should mean you are using CommonJS. In this scenario, you would use "require" to import os

const os = require('os')
console.log(`os.userInfo().username => ${os.userInfo().username}`)

 

On the other hand, if your package.json does have "type": "module" this should mean you are using an ES Module. In this scenario, you would use "import".

import os from 'os'
console.log(`os.userInfo().username => ${os.userInfo().username}`)

 

Which should return something like this.

os.userInfo().username = john.doe

 


 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 8e4435 in the box below so that we can be sure you are a human.