Node.js OS Module
The node:os module tells your program about the computer it is running on: the operating system, the CPU, memory, network interfaces, the current user and a few useful constants. Tools use it to pick sensible defaults (for example, how many worker threads to start), to write cross-platform code, and to print diagnostics.
import os from 'node:os'; // ES module
const os = require('node:os'); // CommonJSNote: Most
osvalues are different on every machine — that is the whole point of the module. The runnable examples below therefore print the type or shape of each value instead of the value itself, so their output is the same everywhere. Run them on your own computer and print the real values too.
System information
| Function | Example value | Meaning |
|---|---|---|
os.platform() |
'linux', 'darwin', 'win32' |
operating system family |
os.type() |
'Linux', 'Darwin', 'Windows_NT' |
OS name |
os.release() |
'6.8.0-45-generic' |
kernel / OS version |
os.arch() |
'x64', 'arm64' |
CPU architecture |
os.hostname() |
'ana-laptop' |
computer name |
os.homedir() |
'/home/ana' |
home folder of the current user |
os.tmpdir() |
'/tmp' |
folder for temporary files |
os.uptime() |
86400 |
seconds since boot |
os.EOL |
'\n' or '\r\n' |
end-of-line marker |
const os = require('node:os');
const info = {
platform: os.platform(),
type: os.type(),
arch: os.arch(),
hostname: os.hostname(),
homedir: os.homedir(),
tmpdir: os.tmpdir(),
uptime: os.uptime(),
};
for (const [key, value] of Object.entries(info)) {
console.log(key.padEnd(9), typeof value);
}
console.log('known platform?', ['linux', 'darwin', 'win32', 'freebsd'].includes(os.platform()));
console.log('EOL is LF or CRLF?', os.EOL === '\n' || os.EOL === '\r\n');platform string
type string
arch string
hostname string
homedir string
tmpdir string
uptime number
known platform? true
EOL is LF or CRLF? true
On your own machine, simply console.log(os.platform(), os.arch()) to see the real values. process.platform and process.arch contain the same information without importing os.
CPU and memory
os.cpus() returns one object per logical CPU core. os.availableParallelism() (Node 18.14+) is the recommended way to ask "how many things can I do at once?" — use it to size a worker pool.
const os = require('node:os');
const cpus = os.cpus();
console.log('cpus is an array:', Array.isArray(cpus));
console.log('fields of one core:', Object.keys(cpus[0]));
console.log('times fields:', Object.keys(cpus[0].times));
const cores = os.availableParallelism();
console.log('at least one core:', cores >= 1);
const total = os.totalmem();
const free = os.freemem();
console.log('free <= total:', free <= total);
const toGiB = (bytes) => (bytes / 1024 ** 3).toFixed(1) + ' GiB';
console.log('formatter works:', toGiB(8 * 1024 ** 3));cpus is an array: true
fields of one core: [ 'model', 'speed', 'times' ]
times fields: [ 'user', 'nice', 'sys', 'idle', 'irq' ]
at least one core: true
free <= total: true
formatter works: 8.0 GiB
On a real machine, a typical diagnostic line looks like this:
node -e "const os=require('os');console.log(os.availableParallelism()+' cores, '+(os.totalmem()/1024**3).toFixed(1)+' GiB RAM')"8 cores, 15.5 GiB RAM
(Simulated — your numbers will differ.)
The current user
os.userInfo() returns an object with username, uid, gid, shell and homedir. On Windows, uid/gid are -1 and shell is null.
const os = require('node:os');
const user = os.userInfo();
console.log(Object.keys(user).sort());
console.log(typeof user.username);[ 'gid', 'homedir', 'shell', 'uid', 'username' ]
string
Writing cross-platform code
Branch on os.platform() only when you really have to — prefer the helpers that already hide the differences (path.join, os.EOL, os.tmpdir()).
const os = require('node:os');
function configFolder(platform, home) {
switch (platform) {
case 'win32': return home + '\\AppData\\Roaming\\myapp';
case 'darwin': return home + '/Library/Application Support/myapp';
default: return home + '/.config/myapp';
}
}
console.log(configFolder('win32', 'C:\\Users\\ana'));
console.log(configFolder('darwin', '/Users/ana'));
console.log(configFolder('linux', '/home/ana'));
const lines = ['first', 'second'].join(os.EOL);
console.log(lines.split(os.EOL).length);
console.log(os.constants.signals.SIGINT);C:\Users\ana\AppData\Roaming\myapp
/Users/ana/Library/Application Support/myapp
/home/ana/.config/myapp
2
2
os.constants holds operating-system constants, such as signal numbers (SIGINT is 2) and error codes.
Tip: Writing the platform as a function parameter (as
configFolderdoes) makes the code easy to test: you can check the Windows branch while running on Linux.
Test yourself
Which function is recommended for deciding how many worker threads to start?
os.cpus().speedos.availableParallelism()os.totalmem()os.uptime()
What does os.platform() return on Windows?
'windows''Windows_NT''win32''win64'
Why are os values poor choices for printing in automated test output?
- They are always
undefined - They differ from machine to machine
- They require root access
- They are asynchronous