How to get the operating system's current user id using Node.js?

Published April 4, 2021

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.

// require os module
const os = require("os");

// invoke userInfo() method
const userInfo = os.userInfo();

// get uid property
// from the userInfo object
const uid = userInfo.uid;

console.log(uid); // 20

See the above code live in repl.it.

That's all! 😃

Feel free to share if you found this useful 😃.


Share on: Facebook Twitter