child_process.execFile(file[, args][, options][, callback])
file<string> 要运行的可执行文件的名称或路径。args<string[]> 字符串参数列表。options<Object>cwd<string> | <URL> 子进程的当前工作目录。env<Object> 环境键值对。默认值:process.env。encoding<string> 默认值:'utf8'timeout<number> 默认值:0maxBuffer<number> 允许在 stdout 或 stderr 上的最大数据量(以字节为单位)。如果超出,子进程将被终止,任何输出将被截断。请参见maxBuffer和 Unicode 的注意事项。 默认值:1024 * 1024。killSignal<string> | <integer> 默认值:'SIGTERM'uid<number> 设置进程的用户身份(参见setuid(2))。gid<number> 设置进程的组标识(参见setgid(2))。windowsHide<boolean> 隐藏通常在 Windows 系统上创建的子进程控制台窗口。默认值:false。windowsVerbatimArguments<boolean> 在 Windows 上不会对参数进行引号或转义。在 Unix 上会被忽略。默认值:false。shell<boolean> | <string> 如果为true,将在 shell 中运行command。在 Unix 上使用'/bin/sh',在 Windows 上使用process.env.ComSpec。可以通过字符串指定不同的 shell。参见 Shell 要求 和 默认 Windows shell。默认值:false(不使用 shell)。signal<AbortSignal> 允许使用 AbortSignal 中止子进程。
callback<Function> 在进程终止时被调用并输出结果。- 返回:<ChildProcess>
child_process.execFile() 函数类似于 child_process.exec(),区别在于它默认不会启动一个 shell。相反,指定的可执行文件 file 会直接作为新进程启动,这使得它比 child_process.exec() 略微高效一些。
【The child_process.execFile() function is similar to child_process.exec()
except that it does not spawn a shell by default. Rather, the specified
executable file is spawned directly as a new process making it slightly more
efficient than child_process.exec().】
支持与 child_process.exec() 相同的选项。由于没有启动 shell,因此不支持 I/O 重定向和文件匹配等行为。
【The same options as child_process.exec() are supported. Since a shell is
not spawned, behaviors such as I/O redirection and file globbing are not
supported.】
const { execFile } = require('node:child_process');
const child = execFile('node', ['--version'], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});import { execFile } from 'node:child_process';
const child = execFile('node', ['--version'], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});stdout 和 stderr 参数传递给回调时,将包含子进程的 stdout 和 stderr 输出。默认情况下,Node.js 会将输出解码为 UTF-8,并将字符串传递给回调。可以使用 encoding 选项来指定用于解码 stdout 和 stderr 输出的字符编码。如果 encoding 为 'buffer' 或未识别的字符编码,则会将 Buffer 对象传递给回调。
【The stdout and stderr arguments passed to the callback will contain the
stdout and stderr output of the child process. By default, Node.js will decode
the output as UTF-8 and pass strings to the callback. The encoding option
can be used to specify the character encoding used to decode the stdout and
stderr output. If encoding is 'buffer', or an unrecognized character
encoding, Buffer objects will be passed to the callback instead.】
如果以其 util.promisify()ed 版本调用此方法,它将返回一个带有 stdout 和 stderr 属性的 Object 的 Promise。返回的 ChildProcess 实例作为 child 属性附加到该 Promise 上。如果发生错误(包括导致退出代码非 0 的任何错误),将返回一个被拒绝的 Promise,并带有回调中提供的相同 error 对象,但会额外包含两个属性 stdout 和 stderr。
【If this method is invoked as its util.promisify()ed version, it returns
a Promise for an Object with stdout and stderr properties. The returned
ChildProcess instance is attached to the Promise as a child property. In
case of an error (including any error resulting in an exit code other than 0), a
rejected promise is returned, with the same error object given in the
callback, but with two additional properties stdout and stderr.】
const util = require('node:util');
const execFile = util.promisify(require('node:child_process').execFile);
async function getVersion() {
const { stdout } = await execFile('node', ['--version']);
console.log(stdout);
}
getVersion();import { promisify } from 'node:util';
import child_process from 'node:child_process';
const execFile = promisify(child_process.execFile);
async function getVersion() {
const { stdout } = await execFile('node', ['--version']);
console.log(stdout);
}
getVersion();如果启用了 shell 选项,请不要将未经过滤的用户输入传递给此函数。任何包含 shell 元字符的输入都可能被用来触发任意命令的执行。
如果启用了 signal 选项,那么对相应的 AbortController 调用 .abort() 类似于对子进程调用 .kill(),只是传递给回调的错误将是 AbortError:
【If the signal option is enabled, calling .abort() on the corresponding
AbortController is similar to calling .kill() on the child process except
the error passed to the callback will be an AbortError:】
const { execFile } = require('node:child_process');
const controller = new AbortController();
const { signal } = controller;
const child = execFile('node', ['--version'], { signal }, (error) => {
console.error(error); // an AbortError
});
controller.abort();import { execFile } from 'node:child_process';
const controller = new AbortController();
const { signal } = controller;
const child = execFile('node', ['--version'], { signal }, (error) => {
console.error(error); // an AbortError
});
controller.abort();