How Node really works · Chapter 3 of 34
The process object
By the end of this chapter you can write a command line script that reads what was typed, keeps results and errors apart, and leaves the right exit code behind. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: How Node runs
- 5 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
Ravi runs the stock room from a laptop. Every evening he types a command like node restock.js --item milk --qty 3, and he wants the restock line on screen, a clear message when he types it wrong, and the log saved in the folder he is standing in. Kabir's nightly job runs the same script and looks only at its exit code to know whether it worked. And the rider notifier prints nothing at all on the server.
So far your code has never asked who started it. It cannot read what Ravi typed, tell a result from a complaint, or tell the shell it failed. All of that goes through one global object: process.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
parseFlags: turn command line words into flags and positionals
Write parseFlags(argv). argv is shaped like process.argv: skip its first two entries (node and the script) and read the rest in order. It returns an object with _ (the positional words, in order, always there even when empty) and one key per flag.
--name value gives name: 'value'. --name=value gives name: 'value', split on the first = only, so the value may hold another =. --name followed by another --flag, or by nothing, gives name: true. Any word that does not start with -- is positional. Values stay strings, and with nothing after the script the result is { _: [] }.
- Task 2
restock.js: print Ravi's restock line
restock.js reads --item and --qty from its command line. The quantity comes from --qty, else from the RESTOCK_QTY environment variable, else it is 1; --qty wins when both are given.
It prints exactly one line on stdout, Restock: <qty> x <item> (node restock.js --item milk --qty 3 prints Restock: 3 x milk), and exits with code 0.
- Task 3
restock.js: report bad input on stderr, with exit code 2
With no --item, restock.js prints a line containing Missing --item on stderr. With a quantity that is not a whole number above 0 (lots, 2.5, or 0 from RESTOCK_QTY), it prints a line that mentions qty on stderr. Either way it prints nothing on stdout and exits with code 2; a quantity of 0 is a mistake, not a reason to use the default.
On every run, good or bad, it prints restock exited with code <code> on stderr as it exits, so a good run ends with restock exited with code 0 on stderr and has only the restock line on stdout.
- Task 4
restock.js: save the restock log where Ravi is standing
With --out <file>, restock.js also appends the line <qty> x <item> and a newline to that file, and still prints the restock line and exits with 0. A relative file name is relative to the folder the command was started in, not to the script's own folder.
Each run adds one line at the end: two runs from the same folder, for 3 milk and then 2 sugar, leave the file holding 3 x milk and 2 x sugar, one per line.
- Task 5 · interview question
Interview: fix notify.js, which exits before the rider is notified
notify.js must print Notified <name> on stdout and exit with 0 when a name is given (node notify.js Ravi prints Notified Ravi). Today it prints nothing and still exits with 0.
Without a name it must print a line containing Who should I notify? on stderr, print nothing at all on stdout, and exit with 1.
What you will learn
- process.argv and process.env: the words typed after the script, and the environment it was started in.
- stdout, stderr and exit codes: where results and problems go, and how the shell learns that a script failed.
- The exit event and process.cwd(): last words before the process ends, and the folder the command was started in.
More in How Node runs
Build The process object 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.