How Node really works · Chapter 21 of 34

The os module

By the end of this chapter you can read what a machine is made of and how busy it is with node:os, and turn the numbers into a report people can read. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The system
  • 6 tasks
  • Node.js
  • In the all labs plan

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.

Why you need this

Every day at the 4 pm rush the order app slows down, and Ravi is sure "the server is tired". Kabir wants numbers instead of feelings: how many cores the machine has, how busy they are, how much memory is left, how long since it restarted. Meera will open the health report on her phone during the rush.

process tells you about your own Node program, not the computer under it. Without node:os you would shell out to free, uptime and nproc and parse their text, which breaks the day the server runs a different system.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    machineSummary: describe the machine the server runs on

    Write machineSummary(). It returns { platform, arch, cpus, hostname }, read from node:os when it is called: the platform and architecture names, cpus as the number of entries os.cpus() lists (not availableParallelism, which can be smaller in a container), and the hostname.

  2. Task 2

    memoryUsage: turn memory bytes into a readable report

    Write memoryUsage(total, free), both in bytes, with total os.totalmem() and free os.freemem() when left out. It returns { totalMb, freeMb, usedPercent }: totalMb and freeMb are bytes divided by 1024 twice, rounded to the nearest whole number, and usedPercent is the share of total that is not free, as a percentage rounded to the nearest whole number (0 when everything is free).

  3. Task 3

    formatUptime: show how long the machine has been up

    Write formatUptime(seconds), with seconds os.uptime() when left out. It returns the time as whole days, hours and minutes in the form '1d 2h 3m'; the leftover seconds, and any fraction of a second, are dropped, never rounded up. Leading units that are 0 are left out ('1h 0m', '10m'), but minutes always appear ('0m' for 59 seconds), and once days appear, hours appear too ('3d 0h 0m').

  4. Task 4

    toReportText: end report lines the way this platform expects

    Write toReportText(lines, eol), with eol os.EOL when left out. It returns one string in which every line is followed by eol, the last line included. An empty list gives ''.

  5. Task 5

    tapriPaths: config at home, scratch files in the temp folder

    Write tapriPaths(name). It returns { config, scratch }: config is the file config.json inside a folder .tapri in the user's home folder, and scratch is a folder named tapri-<name> in the temp folder. Read both folders from node:os and build the paths with join from node:path; never type a folder in.

  6. Task 6

    healthReport: the health report with warnings for load and memory

    Write healthReport(stats), with stats readStats() when left out. stats is { cpus, loadavg, totalmem, freemem, uptime }. It returns { status, problems, cpus, loadPerCpu, memory, uptime }:

    loadPerCpu is the 1 minute load (the first number in loadavg) divided by cpus, rounded to two decimals; memory is memoryUsage(totalmem, freemem); uptime is formatUptime(uptime); cpus is passed through.

    problems lists 'high load' when the load per core, before rounding, is above 1 (exactly 1 is fine), then 'low memory' when memory.usedPercent is 90 or more, in that order. status is 'warn' when there is any problem, otherwise 'ok'.

What you will learn

  • The machine: platform, architecture, hostname and cores.
  • Memory, uptime and load: raw numbers, and what they really measure inside a container.
  • Paths and line endings: the home folder, the temp folder and os.EOL, never typed in by hand.
  • Testable defaults: reading the machine only when no value is passed in.

More in The system

Build The os module yourself

Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.