Usage in Deno
import { execFile } from "node:child_process";
execFile(file: string): ChildProcess
The child_process.execFile()
function is similar to 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 exec.
The same options as 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); });
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.
If this method is invoked as its util.promisify()
ed version, it returns
a Promise
for an Object
with stdout
and stderr
properties. The returnedChildProcess
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();
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.
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 { 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();
execFile(file: string,options: ,): ChildProcess
execFile(file: string,args?: readonly string[] | null,): ChildProcess
execFile(file: string,args: readonly string[]
| undefined
| null,options: ,): ChildProcess
execFile(file: string,callback: () => void,): ChildProcess
execFile(file: string,args: readonly string[]
| undefined
| null,callback: () => void,): ChildProcess
execFile(): ChildProcess
options: ExecFileOptionsWithBufferEncoding
execFile(file: string,args: readonly string[]
| undefined
| null,options: ExecFileOptionsWithBufferEncoding,callback: () => void,): ChildProcess
options: ExecFileOptionsWithBufferEncoding
execFile(): ChildProcess
options: ExecFileOptionsWithStringEncoding
execFile(file: string,args: readonly string[]
| undefined
| null,options: ExecFileOptionsWithStringEncoding,callback: () => void,): ChildProcess
options: ExecFileOptionsWithStringEncoding
execFile(): ChildProcess
options: ExecFileOptionsWithOtherEncoding
execFile(file: string,args: readonly string[]
| undefined
| null,options: ExecFileOptionsWithOtherEncoding,callback: () => void,): ChildProcess
options: ExecFileOptionsWithOtherEncoding
execFile(): ChildProcess
options: ExecFileOptions
execFile(file: string,args: readonly string[]
| undefined
| null,options: ExecFileOptions,callback: () => void,): ChildProcess
options: ExecFileOptions
execFile(file: string,options: ,callback: (() => void)
| undefined
| null,): ChildProcess
execFile(file: string,args: readonly string[]
| undefined
| null,options: ,callback: (() => void)
| undefined
| null,): ChildProcess