child_process.execFileSync(file[, args][, options])


  • file <string> 要运行的可执行文件的名称或路径。
  • args <string[]> 字符串参数的列表。
  • options <Object>

    • cwd <string> 子进程的当前工作目录。
    • input <string> | <Buffer> | <TypedArray> | <DataView> 该值将会作为 stdin 传给衍生的进程。提供此值将会覆盖 stdio[0]
    • stdio <string> | <Array> 子进程的 stdio 配置。默认情况下,除非指定了 stdio,否则 stderr 将会被输出到父进程的 stderr。默认值: 'pipe'
    • env <Object> 环境变量的键值对。默认值: process.env
    • uid <number> 设置进程的用户标识,参阅 setuid(2)
    • gid <number> 设置进程的群组标识,参阅 setgid(2)
    • timeout <number> 允许进程运行的最长时间,以毫秒为单位。默认值: undefined
    • killSignal <string> | <integer> 当衍生的进程将被终止时使用的信号值。默认值: 'SIGTERM'
    • maxBuffer <number> stdout 或 stderr 上允许的最大字节数。如果超过限制,则子进程会被终止。参阅 maxBuffer 与 Unicode 中的警告。默认值: 1024 * 1024
    • encoding <string> 用于所有 stdio 输入和输出的字符编码。默认值: 'buffer'
    • windowsHide <boolean> 隐藏子进程的控制台窗口(在 Windows 系统上通常会创建)。默认值: false
    • shell <boolean> | <string> 如果为 true,则在 shell 中运行 command。 在 Unix 上使用 '/bin/sh',在 Windows 上使用 process.env.ComSpec。 可以将不同的 shell 指定为字符串。 参阅 shell 的要求Windows 默认的 shell默认值: false(没有 shell)。
  • 返回: <Buffer> | <string> 命令的 stdout。

child_process.execFileSync() 方法通常与 child_process.execFile() 相同,但该方法在子进程完全关闭之前不会返回。 当遇到超时并发送 killSignal 时,该方法也需等到进程完全退出后才返回。

如果子进程拦截并处理了 SIGTERM 信号但未退出,则父进程仍将等待子进程退出。

如果进程超时或具有非零的退出码,则此方法将抛出一个 Error,其中包含底层 child_process.spawnSync() 的完整结果。

如果启用了 shell 选项,则不要将未经过处理的用户输入传给此函数。 包含 shell 元字符的任何输入都可用于触发任意命令的执行。

  • file <string> The name or path of the executable file to run.
  • args <string[]> List of string arguments.
  • options <Object>

    • cwd <string> Current working directory of the child process.
    • input <string> | <Buffer> | <TypedArray> | <DataView> The value which will be passed as stdin to the spawned process. Supplying this value will override stdio[0].
    • stdio <string> | <Array> Child's stdio configuration. 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.