How Node really works · Chapter 23 of 34

Child processes

By the end of this chapter you can run other programs from Node, read their output and exit codes, and pass user input to them safely. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The system
  • 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 trusts one script, stock-report.js, which prints what is left in the store room. He wants the app to show "only 18 litres of milk left" before the 4 pm rush, and he has already written the code: an exec call with the item from the app's search box pasted into the command. Kabir asks what happens when someone types milk; rm -rf ~ there. He also wants the month's bills added up in a separate Node process, so a bug in billing cannot take the order server down with it.

Everything so far ran inside one Node process. To run another program, whether a script, git or a printer driver, Node has to start a child process and talk to it.

What you build: 5 tasks

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

  1. Task 1

    runStockReport: run the stock report and read its output and exit code

    Write runStockReport(args), with args [] when it is left out. It runs stock-report.js (its full path is STOCK_SCRIPT) with the Node that is running you (process.execPath), using execFile, with args after the script.

    It returns a promise of an object with exactly three keys: code (the exit code), stdout and stderr (the text as printed, newlines included). It resolves even when the report exits with an error code: an unknown item gives code 2, stdout '' and stderr 'unknown item: <item>\n'. With no args, stdout holds every item, one line each.

  2. Task 2

    streamStockLines: stream the report line by line with spawn

    Write streamStockLines(onLine). It runs stock-report.js with no arguments using spawn, and calls onLine once for every line the report prints, in order, without the newline.

    It returns a promise of the exit code (0 for the report), resolved only once the report has finished and every line has gone to onLine.

  3. Task 3 · interview question

    Interview: stockFor, look up stock without letting the shell run user input

    Write stockFor(item). It returns a promise of the count for that item from stock-report.js, as a number, or null when the report does not know the item (it exits with code 2).

    item comes from the app's search box, so it must reach the script as one single argument and never pass through a shell. Input such as milk; touch somefile, milk$(touch somefile) or text with quotes in it is just an unknown item: the answer is null, and nothing inside it may run.

  4. Task 4

    bill-child.js: answer bills over the IPC channel

    bill-child.js runs as a forked child. For every message { type: 'bill', id, orders } from the parent, where orders is an array of { price, qty }, it sends back exactly { type: 'total', id, total }, with the same id and total the sum of price times qty (0 for no orders).

    It answers every bill, in order, and keeps listening: it must not disconnect or exit by itself. When the parent disconnects, it exits with code 0 on its own.

  5. Task 5

    billInChild: fork the bill child, get the total, and let it exit

    Write billInChild(orders). It forks bill-child.js (its full path is BILL_CHILD), sends it one message { type: 'bill', id: 1, orders }, and waits for the reply with type 'total' and id 1. Then it disconnects, so the child exits.

    It returns a promise of { total, exitCode }, resolved only after the child has exited (exitCode is 0). Calling it again forks a new child and works the same way.

What you will learn

  • execFile and spawn: run a program with an array of arguments, and get its output all at once or as a stream.
  • exec and the shell: why one command string with user input in it is dangerous.
  • fork and IPC: a Node child that swaps messages with its parent.
  • Exit codes: how a child says whether it worked.

More in The system

Build "Child processes" 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.