Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

What is spawn in Node JS ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Node.js is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of a web browser. Node.js allows developers to utilize JavaScript to create command-line tools and server-side scripting, which involves running scripts on the server before sending the page to the user’s browser.

Although Node.js operates in a single-thread mode, it handles concurrency using an event-driven architecture. Additionally, it makes it easier to create child processes so that multi-core CPU-based systems can benefit from parallel processing. Using the spawn function, the user can send any arguments to a command that is launched in a new process. Instead of using the same node process, it generates a new one using a command. We usually use it, when we want the child process to send a lot of data back to the parent process. It has the following function signature:

spawn(command[, args][, options])

The following is the description of the parameters used in the function:

  • command: The command which is to be run.
  • args: The list of string arguments that are to be passed to the process.
  • options: The options object. It takes the following parameters:
    • cwd: The current working directory of the child process.
    • env: The environment key-value pairs. It has a default value of process.env.
    • argv0: The value of argument argv[0] sent to the child process. This will be set to command if not specified.
    • stdio: The child process’s stdio configuration.
    • detached: It is a boolean which tells whether the child is to be run independently of its parent process or not.
    • uid: The user identity of the process.
    • gid: The group identity of the process.
    • serialization: This specifies the kind of serialization used for sending messages between processes. It has a default value of ‘JSON’.
    • shell: It is a boolean that if true, runs the command inside of a shell.
    • timeout: It is the maximum amount of time the process is allowed to run. The value given should be in milliseconds.

I will try to explain the syntax and usage of spawn using the following examples.

Example 1: In this example, I am using spawn to run a child process that lists all the files and sub-directories in the current working directory.

Javascript




import { spawn } from 'child_process';
  
const lsProcess = spawn('ls');
lsProcess.stdout.on('data', data => {
    console.log(`stdout:\n${data}`);
})
lsProcess.stderr.on("data", (data) => {
    console.log(`stdout: ${data}`);
});
lsProcess.on('exit', code => {
    console.log(`Process ended with ${code}`);
})


Output:

 

Example 2: In this example, I am running a python child process that prints the Hello World message on the stdout stream.

Javascript




import { spawn } from 'child_process';
  
const pythonProcess = spawn('python3',['hello.py']);
pythonProcess.stdout.on('data', data => {
    console.log(`stdout:\n${data}`);
})
pythonProcess.stderr.on("data", (data) => {
    console.log(`stdout: ${data}`);
});
pythonProcess.on('exit', code => {
    console.log(`Process ended with ${code}`);
})


Output:

 

Reference: https://nodejs.org/api/child_process.html#child_processspawncommand-args-options


My Personal Notes arrow_drop_up
Last Updated : 28 Nov, 2022
Like Article
Save Article
Similar Reads
Related Tutorials