On this page

    M

    process.setuid

    History
    process.setuid(id): void
    Attributes

    The process.setuid(id) method sets the user identity of the process. (See setuid(2).) The id can be passed as either a numeric ID or a username string. If a username is specified, the method blocks while resolving the associated numeric ID.

    import process from 'node:process';
    
    if (process.getuid && process.setuid) {
      console.log(`Current uid: ${process.getuid()}`);
      try {
        process.setuid(501);
        console.log(`New uid: ${process.getuid()}`);
      } catch (err) {
        console.error(`Failed to set uid: ${err}`);
      }
    }
    if (process.getuid && process.setuid) {
      console.log(`Current uid: ${process.getuid()}`);
      try {
        process.setuid(501);
        console.log(`New uid: ${process.getuid()}`);
      } catch (err) {
        console.error(`Failed to set uid: ${err}`);
      }
    }

    This function is only available on POSIX platforms (i.e. not Windows or Android). This feature is not available in Worker threads.