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

by
Jeremy Canfield |
Updated: November 27 2024
| 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