Spawning a Child Process
Letâs enhance our file-watching example program even further by having it spawn a child process in response to a change. To do this, weâll bring in Node.jsâs child-process module and dive into some Node.js patterns and classes. Youâll also learn how to use streams to pipe data around.
To keep things simple, weâll make our script invoke the ls command with the -l and -h options. This will give us some information about the target file whenever it changes. You can use the same technique to spawn other kinds of processes, as well.
Open your editor and enter this:
â | â'use strict'â; |
â | âconstâ fs = require(â'fs'â); |
â | âconstâ spawn = require(â'child_process'â).spawn; ... |
Get Node.js 8 the Right Way now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.