execFile() method
The child_process.execFile() function is does not spawn a shell by default. It is slightly more efficient than child_process.exec() as the specified executable file is spawned directly as a new process.
Syntax:
child_process.execFile(file[, args][, options][, callback])
Parameters:
- file: Accepts a string that specifies the name or path of the file to run.
- args: List of string arguments.
- options: Some of the options available are cwd, env, encoding, shell, timeout etc
- callback: The callback function is called when the process terminates. The arguments to this function are error, stdout, and stderr respectively.
Return Value: Returns an instance of ChildProcess.
Example: In this example, we will see the use of the execFile() method.
javascript
const { execFile } = require( 'child_process' ); // Executes the exec.js file const child = execFile( 'node' , [ 'exec.js' ], (error, stdout, stderr) => { if (error) { throw error; } console.log(stdout); }); |
Output:
Node Child Process
Node is a tool that uses JavaScript and has many useful parts to it. Normally, it does work with one thread at a time, which means it can handle tasks without waiting. However, when there’s a lot of work to be done, we use the child_process module to create additional threads. These extra threads can talk to each other using a built-in messaging system.
The following are the four different ways to create a child process in Nodejs:
Table of Content
- spawn() method
- fork() method
- exec() method
- execFile() method
Above mentioned ways are explained below: