What is spawn in Node JS ?
Last Updated :
28 Apr, 2025
Node JS is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of an internet browser. In this article, we will learn about the Spawn in NodeJs.
Prerequisites
Spawn is a technique furnished by using the child_process module in Node.Js. It is used to spawn new approaches, allowing Node.Js programs to execute external instructions or different scripts as separate techniques. The spawn function creates a new baby manner asynchronously, offering admission to to the input/output streams for reading and writing information. This feature takes in the command to be carried out alongside an array of arguments and extra alternatives as parameters.
Syntax:
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.
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
Similar Reads
What is Node? Node is a JavaScript runtime environment that enables the execution of code on the server side. It allows developers to execute JavaScript code outside of a web browser, enabling the development of scalable and efficient network applications. Table of Content What is Node?Steps to setup the Node App
3 min read
What is Clustering in Node? Clustering in Node refers to a technique used to enhance the performance and scalability of NodeJS applications by utilizing the capabilities of multi-core systems. With clustering, you can create multiple instances of the NodeJS process, known as workers, each running on a separate CPU core. By dis
2 min read
What are Modules in Node.js ? In Node.js Application, a Module can be considered as a block of code that provide a simple or complex functionality that can communicate with external application. Modules can be organized in a single file or a collection of multiple files/folders. Almost all programmers prefer modules because of t
5 min read
What is NestJS? NestJS is a powerful framework for building efficient and scalable server-side applications. Developed with a focus on modern JavaScript and TypeScript, NestJS combines object-oriented programming, functional programming, and reactive programming. In this article, we will learn in-depth about NestJS
3 min read
What is the Purpose of __filename Variable in Node.js ? In Node.js, there are several global variables that are available in all modules. One such variable is __filename. This article delves into the purpose, usage, and practical applications of the __filename variable in Node.js.What is __filename?The __filename variable is a built-in global variable in
3 min read