userid
Look up user and group ids and names in Node.js. Uses native POSIX bindings, not processes.
Last updated 15 days ago by GitHub Actions .
Public Domain · Repository · Bugs · Original npm · Tarball · package.json
$ npm install userid 
SYNC missed versions from official npm registry.

userid

Look up user and group ids and names in Node.js, straight from the system's user database. Native bindings to the POSIX functions (getpwnam_r, getgrgid_r, getgrouplist, ...), not child processes, so it sees the same users as id and getent, including NSS sources like LDAP and SSSD.

CI npm

Installation

npm install userid
# or
bun add userid
pnpm add userid

Prebuilt binaries ship inside the package. There is no install script: nothing compiles, and pnpm and Bun need no build approval.

Platform Architectures
Linux (glibc 2.28+, like Node.js 22: RHEL 8, Debian 10, Ubuntu 20.04, ...) x64, arm64, armv7, ppc64le, s390x
Linux (musl: Alpine) x64, arm64
macOS 11+ arm64, x64
FreeBSD x64

On any other platform, importing userid throws an error with code: 'ERR_USERID_NO_PREBUILT_BINARY'. Open an issue to ask for a prebuilt binary, or build from source.

Requires Node.js 22.12 or newer. The binary uses Node-API, so it keeps working on new Node.js versions without a rebuild. It also loads in Bun.

Windows

Windows has no POSIX user database. The package installs anyway (so cross-platform projects can depend on it), isSupported is false, and every lookup throws an error with code: 'ERR_USERID_UNSUPPORTED_PLATFORM'.

Usage

import { ids, uid, gid, gids, username, groupname, isSupported } from 'userid';
// or: const userid = require('userid');

ids('root'); // { uid: 0, gid: 0 }
uid('root'); // 0
gid('wheel'); // 0 on macOS and BSDs (on most Linux distributions, group 0 is 'root')
gids('root'); // e.g. [0, 1, 2]: every group the user is in, primary group included
username(0); // 'root'
groupname(0); // 'root' on Linux, 'wheel' on macOS and BSDs
Function Returns Not found
ids(username) { uid, gid } (gid is the primary group) username not found
uid(username) uid username not found
gid(groupname) gid groupname not found
gids(username) array of gids, no duplicates username not found
username(uid) user name uid not found
groupname(gid) group name gid not found
isSupported false on Windows, else true

Errors:

  • Not found: Error with code: 'ERR_USERID_NOT_FOUND'.
  • Wrong argument type or count: TypeError. A name containing a null byte is a TypeError too.
  • An id that isn't an integer from 0 to 4294967295: RangeError.
  • The system lookup itself failed (an unreachable LDAP server, say): an Error shaped like Node's own, e.g. code: 'EIO', syscall: 'getpwnam_r'.

Every function is safe to call from several worker threads at once.

Promises

A lookup against a network directory (LDAP, SSSD, NIS) can take a long time, and the functions above block the event loop while it runs. The same functions also come as promises, which run the lookup on the libuv thread pool instead:

import { ids, uid, gid, gids, username, groupname } from 'userid/promises';
// or: const { promises } = require('userid');

await ids('root'); // { uid: 0, gid: 0 }
await username(0); // 'root'

They return the same values and fail with the same errors as the synchronous functions, except that every error is a rejected promise. That includes a bad argument (TypeError, RangeError), and ERR_USERID_UNSUPPORTED_PLATFORM on Windows: they never throw synchronously.

A lookup can't be cancelled. The C library has no way to interrupt one, so it holds its thread until the system answers. If you need a deadline, race the lookup against a timer. The lookup itself keeps running in the background, and its thread stays busy until it finishes. That's the same tradeoff as dns.lookup(), which shares the pool: libuv starts 4 threads, so a few hung lookups can stall fs, dns.lookup() and crypto too. If your directory might hang, raise UV_THREADPOOL_SIZE.

Building from source

Needs a C++17 compiler, make and Python (see node-gyp), plus Bun or npm.

git clone https://github.com/cinderblock/node-userid.git
cd node-userid
bun install            # or: npm install
bun run build          # compiles the binary into prebuilds/, and the TypeScript
cd /path/to/your/project
npm install /path/to/node-userid

The package then loads that binary like any other prebuilt one.

Development

bun install          # installs and compiles the TypeScript
bun run build:native # compiles the native addon from native/ into prebuilds/ (not on Windows)
bun run test         # node:test, against `id`
bun run check        # formatting, spelling, types, clang-tidy
bun run check:package  # publint + are-the-types-wrong

Tests run on Node.js directly, which strips the TypeScript types itself. On Windows, skip build:native: the tests check the unsupported-platform behavior there. Use WSL for the rest.

Optional test fixtures, which CI creates on Linux and FreeBSD:

Variable Meaning
LONG_USERNAME_TEST, LONG_GROUPNAME_TEST names over 15 characters
BIG_ID_USERNAME, BIG_ID_GROUPNAME, BIG_ID a user and group with an id above 2³¹ (default 3000000000)
USERID_PACKAGE_DIR test an installed copy of the package instead of this checkout

test/event-loop.test.mts checks that the promise API leaves the event loop free during a slow lookup. It needs a slow getpwnam_r, so it only runs with USERID_SLOW_LOOKUP_MS set and test/fixtures/slow-getpwnam.c preloaded (glibc Linux; CI runs it):

cc -shared -fPIC -o /tmp/slow-getpwnam.so test/fixtures/slow-getpwnam.c -ldl
LD_PRELOAD=/tmp/slow-getpwnam.so USERID_SLOW_LOOKUP_MS=300 node --test test/event-loop.test.mts

scripts/smoke-test-package.sh <tarball> <npm|pnpm|bun> installs a packed tarball into a scratch project and runs the tests against it. Run bun run build first.

History

This package was originally created by Jen Andre jandre@gmail.com.

In 2019, it was, unfortunately, missing updates that are required to run on the latest versions of Node.js.

Since Jen has not responded to any attempts at contact, I requested that Npm transfer the package so that I might keep it maintained.

Version 1.0.0 switched to N-API, the forward compatible interface that should be, all around, much nicer moving forward. It also signals the change to using Github Actions to run all our full coverage tests on all supported platforms.

Version 2.0.0 ships prebuilt binaries, TypeScript types, ES module and CommonJS entry points, and installs on Windows. Version 2.1.0 adds the promise API. See the changelog. The prebuild matrix started from @matthewh's #8.

Releasing

Releases are published by CI, never from a developer machine. Bump the version in package.json, commit, then push a matching tag (git tag v1.2.3 && git push origin v1.2.3). The workflow runs all of CI, builds the prebuilt binaries, tests the packed tarball, and then publishes that exact tarball with provenance through npm trusted publishing.

License

The license, when the package was created, had some mix of GPLv3 and Public Domain.

Since the main source code has always been Public Domain, I believe it should stay that way. I've also explicitly extended it to the rest of the main (published) source code.

Even though Jen's before last commit changed the license in one location to MIT, the tests, build files, and other scripts (especially those by other contributors) were added under the GPLv3. I'm therefore inclined to think it's better to not hold to that final license change.

Going forward, all published source code (the part that you use as a user of this package) will be all Public Domain. The rest of this package will stay GPLv3.

© 2021 Cameron Tacklind cameron@tacklind.com

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Current Tags

  • 2.1.0                                ...           latest (15 days ago)

29 Versions

  • 2.1.0                                ...           15 days ago
  • 2.0.0                                ...           17 days ago
  • 1.3.0                                ...           17 days ago
  • 1.2.5                                ...           5 years ago
  • 1.2.4                                ...           5 years ago
  • 1.2.3                                ...           5 years ago
  • 1.2.2                                ...           5 years ago
  • 1.2.1                                ...           5 years ago
  • 1.2.0                                ...           6 years ago
  • 1.1.2                                ...           6 years ago
  • 1.1.1                                ...           6 years ago
  • 1.1.0                                ...           6 years ago
  • 1.0.0-beta.9                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
  • 1.0.0-beta.8                                ...           6 years ago
  • 1.0.0-beta.6                                ...           7 years ago
  • 1.0.0-beta.5                                ...           7 years ago
  • 1.0.0-beta.3                                ...           7 years ago
  • 1.0.0-beta.2                                ...           7 years ago
  • 1.0.0-beta.1                                ...           7 years ago
  • 1.0.0-beta                                ...           7 years ago
  • 0.4.0                                ...           7 years ago
  • 0.3.1                                ...           10 years ago
  • 0.3.0                                ...           10 years ago
  • 0.2.0                                ...           11 years ago
  • 0.1.3                                ...           11 years ago
  • 0.1.2                                ...           12 years ago
  • 0.1.1                                ...           13 years ago
  • 0.1.0                                ...           14 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (9)
Dependents (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE