Skip to main content

Node

Runtime questions about Node itself — is this Node, which major version — plus requireOptional for modules that may not be installed. Process-level questions such as uptime, pid and cwd live in process. The version helpers are for guarding features that need a floor and failing with a clear message instead of a cryptic crash deeper in. requireOptional returns undefined rather than throwing, which is the behaviour you want around optional peer dependencies.

Example

import { getNodeMajorVersion, isNode, requireOptional } from '@rtorcato/js-common/node'

// Optional peer dependency: degrade instead of crashing on a missing module.
const pretty = requireOptional('pino-pretty')
if (!pretty) console.warn('pino-pretty not installed — logging as JSON')

// Fail at boot with a clear message rather than deep inside a request.
if (isNode() && getNodeMajorVersion() < 20) throw new Error('Node 20+ required')

Import

import { getNodeMajorVersion, isNode, nodeVersionCheck } from '@rtorcato/js-common/node'

Exports

NameSummary
getNodeMajorVersionReturns the current Node.js major version as a number.
isNodeChecks if the current environment is Node.js.
nodeVersionCheckChecks if the current Node.js version is less than the specified version.
requireOptionalAttempts to require a module, returning undefined if the module is not found.

See also

  • process — cwd, pid, uptime, exit, CI detection
  • os — platform, arch, home and temp directories
  • env — read env vars and branch on NODE_ENV
  • system — detect OS, mobile platform and touch support