Full answer
os module, about the machine:
- os.availableParallelism(): how many tasks can run in parallel (added in Node 18.14 and 19.4). Prefer it over os.cpus().length, which counts logical CPUs even when a container is limited.
- os.totalmem(), os.freemem(): memory in bytes.
- os.platform(), os.arch(), os.release(), os.hostname().
- os.homedir(), os.tmpdir(): standard locations.
- os.EOL: the line ending of the platform.
process object, about the running program:
- process.argv: command-line arguments. The first two are the Node path and the script path.
- process.env: environment variables, always strings.
- process.version and process.versions: Node and dependency versions.
- process.memoryUsage(): rss, heapTotal, heapUsed, external.
- process.cwd(), process.pid, process.uptime(), process.exitCode.
Container warning: inside Docker or Kubernetes, os.totalmem() may show the host's memory, not your limit. Read cgroup limits or process.constrainedMemory() when sizing caches.
For a worker pool, start with os.availableParallelism() for CPU-bound jobs, and a larger number for jobs that mostly wait on I/O. Then measure.
const os = require('node:os');
console.log('availableParallelism is a positive integer:', Number.isInteger(os.availableParallelism()) && os.availableParallelism() > 0);
console.log('total memory is more than free memory:', os.totalmem() > os.freemem());
console.log('platform is a string:', typeof os.platform());
console.log('EOL is a line break:', os.EOL === '\n' || os.EOL === '\r\n');
console.log('tmpdir is absolute:', require('node:path').isAbsolute(os.tmpdir()));
process.env.PASS_MARK = 40;
console.log('env value type:', typeof process.env.PASS_MARK, JSON.stringify(process.env.PASS_MARK));
console.log('argv length (node path + script path):', process.argv.length);
console.log('Node major version is a number:', Number.isInteger(Number(process.versions.node.split('.')[0])));
const { rss, heapUsed } = process.memoryUsage();
console.log('rss is larger than heapUsed:', rss > heapUsed);The snippet prints checks instead of raw values, because the real values differ between machines. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): availableParallelism is a positive integer: true total memory is more than free memory: true platform is a string: string EOL is a line break: true tmpdir is absolute: true env value type: string "40" argv length (node path + script path): 2 Node major version is a number: true rss is larger than heapUsed: true
Key points
- os describes the machine. process describes the running program.
- Use os.availableParallelism() to size pools.
- process.env values are always strings.
- Container limits may differ from what os reports.
Common mistakes
- Assuming process.env.PORT is a number.
- Using os.cpus().length in a container with a CPU limit.
- Trusting os.totalmem() as the memory limit of a container.
- Putting secrets in process.argv, where other users can see them with ps.
Follow-ups
- How do you pass the port number from the command line?
- What does rss mean compared with heapUsed?
- How would you detect that Node runs inside a container?
Tips
- Mention the string-only rule of process.env.
- Name availableParallelism, because older answers say os.cpus().length.
- Add the container caveat to show production awareness.