process Example (spawn child)Start a child process using the spawn method, pass arguments to the process, and print its output.
This page was last reviewed on Dec 21, 2023.
Process. It is common for a Node.js application to need to run an external process, like to encode images. With the child_process module, we can spawn subprocesses.
By calling the on() method on stdout, we can access a Buffer that has the output of the process. This is what would have been printed in the terminal window.
Example. We require the node child_process module to access useful methods like spawn(). To run this example, be sure to install cwebp (a program for generating WEBP images).
Step 1 We specify the input and output file names that we pass as arguments to cwebp. These are relative to the home directory.
Step 2 We call the spawn method. The first argument is the command we want to run, and the second is an array of arguments.
Step 3 With stdout.on() we receive a Buffer full of the output bytes when data has been printed.
Step 4 We use stderr.on() in the same was as stdout.on, but it receives the data written to the error log.
Step 5 When the close event occurs, we print the exit code of the process. The lambda receives an integer code.
const c = require("node:child_process"); // Step 1: specify the input and output file names to cwebp. const inputImage = "test/arrow.png"; const outputImage = "test/arrow.webp"; // Step 2: spawn the process and specify command-line arguments as an array. const cwebp = c.spawn("cwebp", ["-q", "30", inputImage, "-o", outputImage]); // Step 3: if stdout has data, print it as a string. cwebp.stdout.on("data", (data) => { console.log(data.toString()); }); // Step 4: if stderr has data, print it as a string. cwebp.stderr.on("data", (data) => { console.error(data.toString()); }); // Step 5: when child process is closed, exit and print the exit code. cwebp.on("close", (code) => { console.log("Exited with code:", code); });
Saving file 'test/arrow.webp' File: test/arrow.png Dimension: 245 x 127 Output: 818 bytes Y-U-V-All-PSNR 38.09 37.89 37.14 37.88 dB (0.21 bpp) block count: intra4: 28 (21.88%) intra16: 100 (78.12%) skipped: 41 (32.03%) bytes used: header: 39 (4.8%) mode-partition: 180 (22.0%) Residuals bytes |segment 1|segment 2|segment 3|segment 4| total macroblocks: | 4%| 14%| 29%| 53%| 128 quantizer: | 68 | 66 | 59 | 42 | filter level: | 24 | 18 | 26 | 6 | Exited with code: 0
Process versus child process. In Node, there is a process module as well as a child_process module. Process refers to the current process, and child_process is a subprocess.
Summary. With the spawn method, we can run subprocesses in Node.js. This is essential for certain tasks like encoding images with a separate program.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 21, 2023 (new).
© 2007-2024 Sam Allen.