How to handle Child Threads in Node.js ? Last Updated : 10 Sep, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Node.js is a single-threaded language and uses the multiple threads in the background for certain tasks as I/O calls but it does not expose child threads to the developer.But node.js gives us ways to work around if we really need to do some work parallelly to our main single thread process.Child Process in Node: The child_process module gives the node the ability to run the child process by accessing operating system commands.Example: Filename: parallelProcess.js javascript // Do any work in parallel to main // event loop or main process console.log('Child Process Starts') setTimeout(() => { console.log('Data processed') }, 5000) Filename: main.js javascript const { fork } = require('child_process'); // Fork another process const child_process = fork('./parallelProcess.js'); // Data we may need to send to the child process const data = {} console.log('Before process') // Send the data to forked process child_process.send({ data }, function(){ console.log('Sending data') }); // Listen to forked process child_process.on('close', (result) => { console.log('Child process terminated and returned'); }); console.log('After process') Output: Before process After process Child Process Starts Data processed Child process terminated and returned Worker Threads The worker_threads module enables the use of threads that execute JavaScript in parallel. Worker's threads are useful for performing CPU-intensive JavaScript operations. They will not help much with I/O-intensive work which can be done better using Node.js built-in asynchronous I/O operations.Example: javascript const {Worker, isMainThread, parentPort} = require('worker_threads'); if (isMainThread) { // Main code, only executed for main thread const worker = new Worker(__filename, { workerData: {} }); worker.on('message', (m) => console.log( 'Thread send message:', m)); worker.on('error', () => console.log('Error')); worker.on('exit', () => { console.log('Worker exit') }); } else { // Worker thread code, only execute // for working thread. setTimeout(() => { parentPort.postMessage('Hello World!'); }, 3000) } Output: Thread send message: Hello World! Worker exit The process has its own memory space on other hand, threads use the shared memory space. Thread is part of the process. Since worker_threads make new threads inside the same process it requires fewer resources.Reference: https://nodejs.org/api/child_process.html https://nodejs.org/api/worker_threads.html Comment More infoAdvertise with us Next Article How to Exit Process in Node.js ? B bjindal Follow Improve Article Tags : Web Technologies Node.js Node.js-Misc Similar Reads How to read and write files in Node JS ? NodeJS provides built-in modules for reading and writing files, allowing developers to interact with the file system asynchronously. These modules, namely fs (File System), offer various methods for performing file operations such as reading from files, writing to files, and manipulating file metada 2 min read How to Handle Errors in Node.js ? Node.js is a JavaScript extension used for server-side scripting. Error handling is a mandatory step in application development. A Node.js developer may work with both synchronous and asynchronous functions simultaneously. Handling errors in asynchronous functions is important because their behavior 4 min read How to handle concurrency in Node.js ? Node.js is an open-source, cross-platform runtime environment built on Chrome's V8 Engine. It is used to develop highly scalable backend as well as server-side applications. Node.js uses a single-threaded event loop architecture. It is also asynchronous in nature. These are the two main reasons why 4 min read How to Exit Process in Node.js ? In this article, we will see how to exit in NodeJS application. There are different types of methods to exit in Nodejs application, here we have discussed the following four methods. Table of Content Using ctrl+C keyUsing process.exit() FunctionUsing process.exitCode variableUsing process.on() Funct 3 min read How to handle asynchronous operations in Node ? NodeJS, renowned for its asynchronous and event-driven architecture, offers powerful mechanisms for handling asynchronous operations efficiently. Understanding how to manage asynchronous operations is crucial for NodeJS developers to build responsive and scalable applications. What are Asynchronous 2 min read If Node.js is single threaded then how to handles concurrency ? Node js is an open-source virtual machine that uses javascript as its scripting language. Despite being single-threaded, it is one of the most popular web technologies. The reason why node js is popular despite being single-threaded is the asynchronous nature that makes it possible to handle concurr 4 min read Like