The os Module

Info About the Machine Node Is Running On

The os Module

os tells you about the host — CPUs, memory, hostname, temp directory.

2 min read Level 1/5 #nodejs#os#system
What you'll learn
  • Get system info
  • Use os.tmpdir() for temp files
  • Pick the right concurrency

node:os exposes info about the host machine — its CPUs, memory, hostname, network interfaces, temp directory.

Common Calls

import * as os from "node:os";

os.hostname();        // 'my-mac.local'
os.platform();        // 'darwin' / 'linux' / 'win32'
os.arch();            // 'x64' / 'arm64'
os.release();         // OS version string

os.cpus().length;     // number of logical cores
os.totalmem();        // total RAM in bytes
os.freemem();         // free RAM in bytes

os.tmpdir();          // '/tmp' or platform equivalent
os.homedir();         // '/home/me' or '/Users/me'
os.uptime();          // seconds since boot

Picking Concurrency

How many parallel workers should you spawn? Common pattern: match core count.

import { cpus } from "node:os";
const WORKERS = Math.max(1, cpus().length - 1);

Leave one core for other things — empirically works well.

Temp Files

For scratch files that should live in /tmp:

import { tmpdir } from "node:os";
import { join } from "node:path";
import { writeFile } from "node:fs/promises";

const tmp = join(tmpdir(), `scratch-${Date.now()}.txt`);
await writeFile(tmp, "draft");
console.log("wrote", tmp);

EOL

os.EOL is \n on Unix, \r\n on Windows. Use it when writing text files the OS will read with native tools.

import { EOL } from "node:os";
const lines = ["a", "b", "c"].join(EOL);

Most of the time, you can just use "\n" — modern tools accept it everywhere. Reach for EOL only when interfacing with platform-native tooling.

Buffers →