On this page

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(file, args?, options?): Buffer | string
Attributes
file:string
The name or path of the executable file to run.
args:string[]
List of string arguments.
options:Object
cwd:string | URL
Current working directory of the child process.
The value which will be passed as stdin to the spawned process. If stdio[0] is set to 'pipe', Supplying this value will override stdio[0].
stdio?:string | Array
Child's stdio configuration. See child_process.spawn()'s stdio. stderr by default will be output to the parent process' stderr unless stdio is specified. Default: 'pipe'.
env?:Object
Environment key-value pairs. Default: process.env.
uid:number
Sets the user identity of the process (see setuid(2)).
gid:number
Sets the group identity of the process (see setgid(2)).
timeout?:number
In milliseconds the maximum amount of time the process is allowed to run. Default: undefined.
killSignal?:string | integer
The signal value to be used when the spawned process will be killed. Default: 'SIGTERM'.
maxBuffer?:number
Largest amount of data in bytes allowed on stdout or stderr. If exceeded, the child process is terminated. See caveat at maxBuffer and Unicode. Default: 1024 * 1024.
encoding?:string
The encoding used for all stdio inputs and outputs. Default: 'buffer'.
windowsHide?:boolean
Hide the subprocess console window that would normally be created on Windows systems. Default: false.
shell?:boolean | string
If 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).
Returns:Buffer | string
The stdout from the command.

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
Attributes
command:string
The command to run.
options:Object
cwd:string | URL
Current working directory of the child process.
The value which will be passed as stdin to the spawned process. If stdio[0] is set to 'pipe', Supplying this value will override stdio[0].
stdio?:string | Array
Child's stdio configuration. See child_process.spawn()'s stdio. stderr by default will be output to the parent process' stderr unless stdio is specified. Default: 'pipe'.
env?:Object
Environment key-value pairs. Default: process.env.
shell?:string
Shell to execute the command with. See Shell requirements and Default Windows shell. Default: '/bin/sh' on Unix, process.env.ComSpec on Windows.
uid:number
Sets the user identity of the process. (See setuid(2)).
gid:number
Sets the group identity of the process. (See setgid(2)).
timeout?:number
In milliseconds the maximum amount of time the process is allowed to run. Default: undefined.
killSignal?:string | integer
The signal value to be used when the spawned process will be killed. Default: 'SIGTERM'.
maxBuffer?:number
Largest amount of data in bytes allowed on stdout or stderr. If exceeded, the child process is terminated and any output is truncated. See caveat at maxBuffer and Unicode. Default: 1024 * 1024.
encoding?:string
The encoding used for all stdio inputs and outputs. Default: 'buffer'.
windowsHide?:boolean
Hide the subprocess console window that would normally be created on Windows systems. Default: false.
Returns:Buffer | string
The stdout from the command.

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(command, args?, options?): Object
Attributes
command:string
The command to run.
args:string[]
List of string arguments.
options:Object
cwd:string | URL
Current working directory of the child process.
The value which will be passed as stdin to the spawned process. If stdio[0] is set to 'pipe', Supplying this value will override stdio[0].
argv0:string
Explicitly set the value of argv[0] sent to the child process. This will be set to command if not specified.
stdio?:string | Array
Child's stdio configuration. See child_process.spawn()'s stdio. Default: 'pipe'.
env?:Object
Environment key-value pairs. Default: process.env.
uid:number
Sets the user identity of the process (see setuid(2)).
gid:number
Sets the group identity of the process (see setgid(2)).
timeout?:number
In milliseconds the maximum amount of time the process is allowed to run. Default: undefined.
killSignal?:string | integer
The signal value to be used when the spawned process will be killed. Default: 'SIGTERM'.
maxBuffer?:number
Largest amount of data in bytes allowed on stdout or stderr. If exceeded, the child process is terminated and any output is truncated. See caveat at maxBuffer and Unicode. Default: 1024 * 1024.
encoding?:string
The encoding used for all stdio inputs and outputs. Default: 'buffer'.
shell?:boolean | string
If 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).
windowsVerbatimArguments?:boolean
No quoting or escaping of arguments is done on Windows. Ignored on Unix. This is set to true automatically when shell is specified and is CMD. Default: false.
windowsHide?:boolean
Hide the subprocess console window that would normally be created on Windows systems. Default: false.
Returns:Object
pid:number
Pid of the child process.
output:Array
Array of results from stdio output.
stdout:Buffer | string
The contents of output[1].
stderr:Buffer | string
The contents of output[2].
status:number | null
The exit code of the subprocess, or null if the subprocess terminated due to a signal.
signal:string | null
The signal used to kill the subprocess, or null if the subprocess did not terminate due to a signal.
error:Error
The error object if the child process failed or timed out.

The 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.