Synchronous process creation
History
The child_process.spawnSync(), child_process.execSync(), and
child_process.execFileSync() methods are synchronous and will block the
Node.js event loop, pausing execution of any additional code until the spawned
process exits.
Blocking calls like these are mostly useful for simplifying general-purpose scripting tasks and for simplifying the loading/processing of application configuration at startup.
child_process.execFileSync
History
cwd option can be a WHATWG URL object using file: protocol.input option can now be any TypedArray or a DataView.windowsHide option is supported now.input option can now be a Uint8Array.encoding option can now explicitly be set to buffer.child_process.execFileSync(file, args?, options?): Buffer | string
stringstring[]Objectstring | Buffer | TypedArray | DataViewstdio[0] is set to 'pipe', Supplying
this value will override stdio[0].child_process.spawn()'s stdio. stderr by default will
be output to the parent process' stderr unless stdio is specified.
Default: 'pipe'.Objectprocess.env.numberundefined.'SIGTERM'.numbermaxBuffer and Unicode. Default: 1024 * 1024.string'buffer'.booleanfalse.true, runs command inside of a shell. Uses
'/bin/sh' on Unix, and process.env.ComSpec on Windows. A different
shell can be specified as a string. See Shell requirements and
Default Windows shell. Default: false (no shell).The child_process.execFileSync() method is generally identical to
child_process.execFile() with the exception that the method will not
return until the child process has fully closed. When a timeout has been
encountered and killSignal is sent, the method won't return until the process
has completely exited.
If the child process intercepts and handles the SIGTERM signal and
does not exit, the parent process will still wait until the child process has
exited.
If the process times out or has a non-zero exit code, this method will throw an
Error that will include the full result of the underlying
child_process.spawnSync().
If the shell option is enabled, do not pass unsanitized user input to this
function. Any input containing shell metacharacters may be used to trigger
arbitrary command execution.
const { execFileSync } = require('node:child_process'); try { const stdout = execFileSync('my-script.sh', ['my-arg'], { // Capture stdout and stderr from child process. Overrides the // default behavior of streaming child stderr to the parent stderr stdio: 'pipe', // Use utf8 encoding for stdio pipes encoding: 'utf8', }); console.log(stdout); } catch (err) { if (err.code) { // Spawning child process failed console.error(err.code); } else { // Child was spawned but exited with non-zero exit code // Error contains any stdout and stderr from the child const { stdout, stderr } = err; console.error({ stdout, stderr }); } }
import { execFileSync } from 'node:child_process'; try { const stdout = execFileSync('my-script.sh', ['my-arg'], { // Capture stdout and stderr from child process. Overrides the // default behavior of streaming child stderr to the parent stderr stdio: 'pipe', // Use utf8 encoding for stdio pipes encoding: 'utf8', }); console.log(stdout); } catch (err) { if (err.code) { // Spawning child process failed console.error(err.code); } else { // Child was spawned but exited with non-zero exit code // Error contains any stdout and stderr from the child const { stdout, stderr } = err; console.error({ stdout, stderr }); } }
child_process.execSync(command, options?): Buffer | string
stringObjectstring | Buffer | TypedArray | DataViewstdio[0] is set to 'pipe', Supplying
this value will override stdio[0].child_process.spawn()'s stdio. stderr by default will
be output to the parent process' stderr unless stdio is specified.
Default: 'pipe'.Objectprocess.env.string'/bin/sh' on Unix, process.env.ComSpec on Windows.numberundefined.'SIGTERM'.numbermaxBuffer and Unicode.
Default: 1024 * 1024.string'buffer'.booleanfalse.The child_process.execSync() method is generally identical to
child_process.exec() with the exception that the method will not return
until the child process has fully closed. When a timeout has been encountered
and killSignal is sent, the method won't return until the process has
completely exited. If the child process intercepts and handles the SIGTERM
signal and doesn't exit, the parent process will wait until the child process
has exited.
If the process times out or has a non-zero exit code, this method will throw.
The Error object will contain the entire result from
child_process.spawnSync().
Never pass unsanitized user input to this function. Any input containing shell metacharacters may be used to trigger arbitrary command execution.
child_process.spawnSync
History
cwd option can be a WHATWG URL object using file: protocol.input option can now be any TypedArray or a DataView.windowsHide option is supported now.input option can now be a Uint8Array.encoding option can now explicitly be set to buffer.shell option is supported now.child_process.spawnSync(command, args?, options?): Object
stringstring[]Objectstring | Buffer | TypedArray | DataViewstdio[0] is set to 'pipe', Supplying
this value will override stdio[0].stringargv[0] sent to the child
process. This will be set to command if not specified.Objectprocess.env.numberundefined.'SIGTERM'.numbermaxBuffer and Unicode.
Default: 1024 * 1024.string'buffer'.true, runs command inside of a shell. Uses
'/bin/sh' on Unix, and process.env.ComSpec on Windows. A different
shell can be specified as a string. See Shell requirements and
Default Windows shell. Default: false (no shell).booleantrue automatically
when shell is specified and is CMD. Default: false.booleanfalse.ObjectnumberArraynull if the
subprocess terminated due to a signal.null if
the subprocess did not terminate due to a signal.ErrorThe child_process.spawnSync() method is generally identical to
child_process.spawn() with the exception that the function will not return
until the child process has fully closed. When a timeout has been encountered
and killSignal is sent, the method won't return until the process has
completely exited. If the process intercepts and handles the SIGTERM signal
and doesn't exit, the parent process will wait until the child process has
exited.
If the shell option is enabled, do not pass unsanitized user input to this
function. Any input containing shell metacharacters may be used to trigger
arbitrary command execution.