The child_process.spawn() method spawns a new process using the givencommand, with command-line arguments in args. If omitted, args defaults
to an empty array.
If the shell option is enabled, do not pass unsanitized user input to thisfunction. Any input containing shell metacharacters may be used to triggerarbitrary command execution.
A third argument may be used to specify additional options, with these defaults:
Use cwd to specify the working directory from which the process is spawned.
If not given, the default is to inherit the current working directory. If given,
but the path does not exist, the child process emits an ENOENT error
and exits immediately. ENOENT is also emitted when the command
does not exist.
Use env to specify environment variables that will be visible to the new
process, the default is process.env.
undefined values in env will be ignored.
Example of running ls -lh /usr, capturing stdout, stderr, and the
exit code:
subprocess.on('error', (err) => { console.error('Failed to start subprocess.'); });
Certain platforms (macOS, Linux) will use the value of argv[0] for the process
title while others (Windows, SunOS) will use command.
Node.js currently overwrites argv[0] with process.execPath on startup, soprocess.argv[0] in a Node.js child process will not match the argv0parameter passed to spawn from the parent,
retrieve it with theprocess.argv0 property instead.
If the signal option is enabled, calling .abort() on the correspondingAbortController is similar to calling .kill() on the child process except
the error passed to the callback will be an AbortError:
const { spawn } = require('child_process'); constcontroller = newAbortController(); const { signal } = controller; constgrep = spawn('grep', ['ssh'], { signal }); grep.on('error', (err) => { // This will be called with err being an AbortError if the controller aborts }); controller.abort(); // Stops the child process
The
child_process.spawn()method spawns a new process using the givencommand, with command-line arguments inargs. If omitted,argsdefaults to an empty array.If the
shelloption is enabled, do not pass unsanitized user input to this function. Any input containing shell metacharacters may be used to trigger arbitrary command execution.A third argument may be used to specify additional options, with these defaults:
Use
cwdto specify the working directory from which the process is spawned. If not given, the default is to inherit the current working directory. If given, but the path does not exist, the child process emits anENOENTerror and exits immediately.ENOENTis also emitted when the command does not exist.Use
envto specify environment variables that will be visible to the new process, the default isprocess.env.undefinedvalues inenvwill be ignored.Example of running
ls -lh /usr, capturingstdout,stderr, and the exit code:Example: A very elaborate way to run
ps ax | grep sshExample of checking for failed
spawn:Certain platforms (macOS, Linux) will use the value of
argv[0]for the process title while others (Windows, SunOS) will usecommand.Node.js currently overwrites
argv[0]withprocess.execPathon startup, soprocess.argv[0]in a Node.js child process will not match theargv0parameter passed tospawnfrom the parent, retrieve it with theprocess.argv0property instead.If the
signaloption is enabled, calling.abort()on the correspondingAbortControlleris similar to calling.kill()on the child process except the error passed to the callback will be anAbortError:Since
v0.1.90