How to check if NodeJS is run by root
Use this function to determine if the current NodeJS process is being run by the root user:
is_current_user_root.js
function isCurrentUserRoot() {
return process.getuid() == 0; // UID 0 is always root
}Usage example:
is_current_user_root_usage.js
if(isCurrentUserRoot()) {
// TODO Your code for root user goes here
} else {
// TODO Your code for NON-root user goes here!
}This works since the UID of the root user is always 0 on Linux/Unix systems.
Note: You don’t need to
process_require.js
const process = require("process");since the process object is automatically imported in any NodeJS environment
Check out similar posts by category:
Javascript, NodeJS
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow