Nodevisor Docs
Packages

@nodevisor/users

Create, remove, and check system users on Linux, macOS, and Windows.

Install

npm install @nodevisor/users

Manage system users with a cross-platform API. Handles user creation, removal, and existence checks.


Quick Start

import $ from '@nodevisor/shell';
import Users from '@nodevisor/users';

const users = $(Users);

// Check current user
const me = await users.whoami();
console.log(me); // "root"

// Create a new user
if (!(await users.exists('runner'))) {
  await users.add('runner');
}

// Remove a user
await users.remove('olduser');

Remote usage:

const $server = $.connect({ host: '10.0.0.10', username: 'root' });

await $server(Users).add('runner');

// Switch to the new user context
const $runner = $server.as('runner');
const whoami = await $runner`whoami`.text(); // "runner"

API

whoami()

Get the username of the current user.

const user = await $(Users).whoami();
console.log(user); // "root"

exists(username)

Check if a user exists on the system. Returns boolean.

if (await $(Users).exists('runner')) {
  console.log('User already exists');
}

add(username)

Create a new system user.

await $(Users).add('runner');
// Linux: useradd runner

remove(username)

Remove a system user.

await $(Users).remove('olduser');
// Linux: userdel olduser

Common Patterns

Create user with SSH key access

import $, { Users, AuthorizedKeys } from 'nodevisor';

const $server = $.connect({ host: '10.0.0.10', username: 'root' });

// Create the user
await $server(Users).add('runner');

// Switch context and add SSH key
const $runner = $server.as('runner');
await $runner(AuthorizedKeys).write(process.env.SSH_PUBLIC_KEY!);

Create user with password

import $, { Users, Auth } from 'nodevisor';

const $server = $.connect({ host: '10.0.0.10', username: 'root' });

await $server(Users).add('runner');
await $server(Auth).setPassword('runner', 'secure-password');

On this page