Child process arguments and lifecycle
Prerequisites: previous curriculum lessons on Node streams, HTTP, buffers and asynchronous errors. Local lab baseline: Node.js 22.16.0, Linux, ESM. Use a disposable directory. Static examples are not ByteMind interactive execution. Each example is independent; save the named files together and run node main.mjs. The author harness imposes a five-second process deadline.
How it works
Use a fixed reviewed executable and a separate argument array with shell disabled. This avoids shell parsing of argument text, but does not make arbitrary executable options or programs safe; validate allowed inputs. The error event can indicate spawn failure, exit reports the process termination, and close reports termination plus closed stdio streams. For buffered execFile output, set maxBuffer and a timeout. For streaming spawn, consume or redirect stdout and stderr, cap output and define a deadline. Use process.execPath for this lab, a fixed script, and a minimal environment. Process separation alone is not a hostile-code sandbox.
Pass a fixed script one argument containing spaces
import {spawn} from 'node:child_process';import {once} from 'node:events';const child=spawn(process.execPath,['task.mjs','two books'],{shell:false,stdio:['ignore','pipe','pipe'],env:{LANG:'C.UTF-8'}});const finished=once(child,'close');let output='',errors='';child.stdout.setEncoding('utf8');child.stdout.on('data',c=>output+=c);child.stderr.on('data',c=>errors+=c);const timer=setTimeout(()=>child.kill(),1500);try {const [code]=await finished;console.log(output.trim());console.log('exit',code,'stderr',errors.length);}finally{clearTimeout(timer);if(child.exitCode===null&&child.signalCode===null)child.kill();}Expected output:
label=two booksexit 0 stderr 0The array contains one label argument. This fixed script cannot interpret that label as shell syntax.
Observe an intentional nonzero child exit
import {execFile} from 'node:child_process';import {promisify} from 'node:util';const run=promisify(execFile);try {await run(process.execPath,['task.mjs'],{timeout:1500,maxBuffer:4096,env:{LANG:'C.UTF-8'}});}catch(error){console.log('exit',error.code);console.log(error.stderr.trim());}Expected output:
exit 2invalid fixtureThe reviewed child explicitly reports an invalid fixture via stderr and a nonzero exit code.
Common mistakes
Avoid interpolating request values into shell strings. Do not use a command runner as a general web endpoint. Do not assume exit code zero guarantees the business result; test the output contract.
Practice
Pass the fixed script a different benign label containing spaces. Explain why the complete label stays one argument when shell is disabled.
Further reading
Official documentation: child_process. Internal reference candidate: node-child-process-arguments-and-lifecycle-lookup (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Child process arguments and lifecycle — knowledge check
Apply the lesson to a fresh scenario and explain the resource or protocol boundary.
Which API directly accepts an executable and argument array for streamed output?
What does the close event establish beyond exit?
Why use shell:false with a fixed executable?