Skip to main content

Os

Runtime: Node.js; degrades in browsers — probes for process / window / navigator and falls back instead of throwing

The handful of operating-system facts worth a named helper — platform, architecture, release, home and temp directories. Each one guards on process being present and returns undefined off Node rather than throwing, so a shared module can call them without a runtime check. They are passthroughs, so you get exactly what Node reports ('darwin', not 'macOS'); for browser-side platform sniffing use system.

For the platform itself use getProcessPlatform.

Example​

import { getHomeDir, getOsArch, getTmpDir } from '@rtorcato/js-common/os'

// Off Node these return `undefined` instead of throwing, so a module shared with
// the browser can call them without a runtime guard.
const cacheDir = `${getHomeDir() ?? getTmpDir()}/.myapp`

getOsArch() // 'arm64' | 'x64' — Node's own strings, unmapped

Import​

import { getHomeDir, getOsArch, getOsRelease } from '@rtorcato/js-common/os'

Exports​

NameSummary
getHomeDirReturns the user's home directory (Node.js only).
getOsArchReturns the OS architecture (Node.js only).
getOsReleaseReturns the OS release/version (Node.js only).
getTmpDirReturns the system's temporary directory (Node.js only).

See also​

  • node — Node version checks and optional requires
  • process — cwd, pid, uptime, exit, CI detection
  • system — detect OS, mobile platform and touch support
  • env — read env vars and branch on NODE_ENV