Skip to content
JavaScript Basics: Inspect OS Capabilities Without Dumping Host Details

JavaScript Basics: Inspect OS Capabilities Without Dumping Host Details

October 27, 2026

A diagnostic probe often needs capabilities rather than hostnames, usernames, or complete network addresses. Node’s os module exposes platform information. This example validates a few useful properties and prints stable checks instead of machine-specific values.

Prepare a scratch directory

Tested on Linux with Node.js v24.19.0. You need the named runtime, Bash, a text editor, and the standard mktemp, rm, and rmdir utilities. The .mjs extension selects ES modules. Assertions check the demonstrated behavior; no npm packages or remote services are needed.

Keep using the same terminal. Stop if setup fails, before saving the source:

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save this as demonstrate-os-inspection.mjs:

import assert from 'node:assert/strict';
import os from 'node:os';
import path from 'node:path';

const parallelism = os.availableParallelism();
assert.ok(Number.isInteger(parallelism) && parallelism > 0);
console.log('Parallelism is a positive integer:', parallelism > 0);
assert.ok(['LE', 'BE'].includes(os.endianness()));
console.log('Recognized byte order:', ['LE', 'BE'].includes(os.endianness()));
assert.ok(path.isAbsolute(os.tmpdir()));
console.log('Temporary-directory path is absolute:', path.isAbsolute(os.tmpdir()));

const total = os.totalmem();
const free = os.freemem();
assert.ok(Number.isFinite(total) && total > 0);
assert.ok(Number.isFinite(free) && free >= 0);
console.log('Memory readings are nonnegative:', total > 0 && free >= 0);
const line = ['alpha', 'beta'].join(os.EOL);
assert.equal(line.split(os.EOL).length, 2);
console.log('Platform line-ending round trip:', line.split(os.EOL).length);

Run it:

node demonstrate-os-inspection.mjs

Observed output:

Parallelism is a positive integer: true
Recognized byte order: true
Temporary-directory path is absolute: true
Memory readings are nonnegative: true
Platform line-ending round trip: 2

Read the result

availableParallelism estimates useful default parallelism and is preferred over simply counting cpus() entries. The byte order check accepts both recognized possibilities. tmpdir() returns a path but does not create a private directory; use a temporary-directory creation API when writing files. Joining and splitting with os.EOL verifies the platform convention without printing an invisible character.

Apply it carefully

These checks intentionally omit exact memory sizes and CPU counts because they vary by host and over time. OS memory figures are not necessarily the container limit or an application’s safe allocation budget. A positive parallelism value is only a starting point for worker tuning, not proof that adding workers will improve throughput. Avoid publishing identifying diagnostic fields unless they are needed and reviewed.

The official reference describes the API and its options. The checks above cover the specific fixture, not every possible input or environment.

Clean up

Remove the source and then the empty practice directory:

rm -- demonstrate-os-inspection.mjs
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

If the directory is not empty, inspect the remaining files rather than deleting unknown contents.

Acknowledgements

This post was created with assistance from AI.

Last updated on