How to execute shell commands in Node.js?


If you ever wanted to run system commands in Node.js? Obviously, the answer is a big “Yes”. If You want to reduce your workload then you have to automate stuff in your production environment. One way to use Bash script to automate things but what will happen if you are not good at Bash or Shell scripting.

With Nodejs, You can execute shell commands and then process their output using javascript. Nodejs provides diffent APIs for doing these operations and there are lot of npm modules to ease the pain creating shell or terminal based CLI’s using Node.js.

In this article, we will go through various ways to execute shell commands in Node.js.

Let’s have a look.

The child_process Module:

As we know, Node.js runs in a single thread. Although You can, take advantage of multiple processes.

child_process module allows us to create child processes in Nodejs. These processes can communicate with each other using a built-in communication channel.

There are 4 different ways to create a child process in Node.

  • exec
  • spawn
  • fork
  • execFile

The exec function

The exec() function creates a new shell and buffers the execution output in memory which can be accessible in the callback function.

The syntax of exec fuction is:

child_process.exec(command[, options][, callback])

Let’s play with exec function to list out all the stuff present in the current directory.

const { exec } = require('child_process');
exec('ls -lrt', (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
  console.log(`stdout: ${stdout}`);
  console.error(`stderr: ${stderr}`);

To use child_process module we need to import via require() function call. Next, we call the exec() function with two parameters:

  • A string of command to run, with space-separated arguments.
  • A callback function with three parameters: error, stdout and stderr

The shell command we are running is ls -lrt, which lists out all the files and folders in our current directory line by line. The callback function logs whether we got an error while trying to execute the command or output on the shell’s stdout or stderr streams.

Note: Never pass unsanitized user input to this function. Any input containing shell metacharacters may be used to trigger arbitrary command execution

If you execute the above code snippet then you will get output as following.

$ node demo.js
stdout: total 120
drwxr-xr-x    5 topcode  staff    160  4 May 12:30 public
drwxr-xr-x    5 topcode  staff    160  4 May 12:30 views
drwxr-xr-x    3 topcode  staff     96  4 May 12:30 bin
-rw-r--r--    1 topcode  staff    330  4 May 12:31 package.json
drwxr-xr-x    4 topcode  staff    128  4 May 12:32 routes
-rw-r--r--    1 topcode  staff   1071  4 May 12:33 app.js
drwxr-xr-x  143 topcode  staff   4576  4 May 12:33 node_modules
-rw-r--r--    1 topcode  staff  41149  4 May 12:33 package-lock.json
-rw-r--r--    1 topcode  staff    691  4 May 12:58 collector.js
-rw-r--r--    1 topcode  staff    239  9 May 11:49 demo.js


That is how can we use exec() function to run a system command in Node.js. Similar to exec() function there are spawn(), execFile(), fork() also do the same thing.

If you want to learn more about the functions then please complete documentation here

If you want to use, third-party npm modules, then check out:

Now you can do shell scripting in Node.js as well. Let me know which application you want to build by using this utilities. Thanks for reading😊

#Happy Coding #topcode

#child_process in nodejs #child process nodejs # child process module in node.js #execute shell commands in node.js #execute system call in node.js #nodejs child_process module #node.js childprocess module #node.js child_process module #child_process APIs #node.js child_process explained #node.js child_process #nodejs child_process #child process in node.js #exec function node.js #spawn in node.js #nodejs exec function #execute shell commands in javascript #execute system commands in node.js #nodejs shell command module #node.js execute #Executing Shell Commands with Node.js #How To Execute Shell Command in Nodejs #How To Execute Shell Command in Node.js #execute command using child process #How to run shell script file or command using Nodejs #How to run shell script file or command using Node.js #run shell command node.js #run shell script node.js

, , , ,

Related posts

Latest posts


Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site