Spawn not found error

spawn not found error › questions › bash-script-spawn-send-interact-commands-n. config.js 94% asset optimizationError: The system cannot find the path specified. at notFoundError ((theme_path) \node_modules\cross-. Hi I am in windows 10 when I issue the command npm run dev. it shows error Error: spawn node_modules/webpack/bin/webpack.js ENOENT at notFoundError.

Spawn not found error - consider

Executing Shell Commands with Node.js


System administrators and developers frequently turn to automation to reduce their workload and improve their processes. When working with servers, automated tasks are frequently scripted with shell scripts. However, a developer might prefer to use a more general higher-level language for complex tasks. Many applications also need to interact with the file system and other OS-level components, which is often more easily done with command line level utilities.

With Node.js, we can run shell commands and process their inputs and outputs using JavaScript. Therefore, we can write most of these complex operations in JavaScript instead of the shell scripting language, potentially making the program easier to maintain.

In this article, we will learn the various ways to execute shell commands in Node.js using the module.

The child_proccess Module

Node.js executes its main event loop in a single thread. However, that does not mean that all of its processing is done in that one thread. Asynchronous tasks in Node.js are executed in other internal threads. When they are complete, the code in the callback, or error, is returned to the main, single thread.

These various threads are run in the same Node.js process. However, it is sometimes desirable to create another process to execute code. When a new process is created, the Operating System determines which processor it uses and how to schedule its tasks.

The module creates new child processes of our main Node.js process. We can execute shell commands with these child processes.

Using external processes can improve performance of your application if used correctly. For example, if a feature of a Node.js application is CPU intensive, as Node.js is single threaded it would block the other tasks from executing while it is running.

However, we can delegate that resource intensive code to a child process, let's say a very efficient C++ program. Our Node.js code will then execute that C++ program in a new process, not blocking its other activities, and when complete process its output.

Two functions that we will use to execute shell commands are and .

The exec Function

The function creates a new shell and executes a given command. The output from the execution is buffered, which means kept in memory, and is available for use in a callback.

Let's use function to list all folders and files in our current directory. In a new Node.js file called , write the following code:

First, we require the module in our program, specifically using the function (via ES6 destructuring). Next, we call the function with two parameters:

  • A string with the shell command we want executed.
  • A callback function with three parameters: , , .

The shell command we are running is , which should list all the files and folders in our current directory line by line, including hidden files/folders. The callback function logs whether we got an while trying to execute the command or output on the shell's or streams.

Note: The object is different from . The object is not null when the module fails to execute a command. This could happen if you try to execute another Node.js script in but the file could not be found, for example. On the other hand, if the command successfully runs and writes a message to the standard error stream, then the object would not be null.

If you run that Node.js file, you should see output similar to:

Now that we've understood how to run commands with , let's learn another way to execute commands with .

The spawn Function

The function executes a command in a new process. This function uses a Stream API, so its output of the command is made available via listeners.

Similar to before, we will use the function to list all folders and files in our current directory. Let's create a new Node.js file, , and enter the following:

We begin by requiring the function from the module. Then, we create a new process that executes the command, passing as an argument. Note how the arguments are held in an array and not included in the command string.

We then set up our listeners. The object of , fires a event when the command writes to that stream. Similarly, the also fires a event when the command writes to that stream.

Errors are caught by listening for them directly on the object that stores the reference for the command. You will only get an error if fails to run the command.

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

The event occurs when the command has finished.

If we run this Node.js file, we should get output like before with :

When to use exec and spawn?

The key difference between and is how they return the data. As stores all the output in a buffer, it is more memory intensive than , which streams the output as it comes.

Generally, if you are not expecting large amounts of data to be returned, you can use for simplicity. Good examples of use-cases are creating a folder or getting the status of a file. However, if you are expecting a large amount of output from your command, then you should use . A good example would be using command to manipulate binary data and then loading it in to your Node.js program.


Node.js can run shell commands by using the standard module. If we use the function, our command will run and its output will be available to us in a callback. If we use the module, its output will be available via event listeners.

If our application expects a lot of output from our commands, we should prefer over . If not, we might opt to use for its simplicity.

Now that you can run tasks external to Node.js, what applications would you build?

Was this article helpful?

spawn - command not found!

Your script is attempting to combine two interpreters. You have both and . That won't work. You can only use one of the two. Since was first, your script is being run as a bash script.

However, within your script, you have commands such as and . Since the script is being read by and not by , this fails. You could get around this by writing different scripts and calling them from your script or by translating the whole thing to .

The best way though, and one that avoids the horrible practice of having your passwords in plain text in a simple text file, is to set up passwordless ssh instead. That way, the won't need a password and you have no need for :

  1. First, create a public ssh key on your machine:

    You will be asked for a passphrase which you will be asked to enter the first time you run any ssh command after each login. This means that for multiple ssh or scp commands, you will only have to enter it once. Leave the passphrase empty for completely passwordless access.

  2. Once you have generated your public key, copy it over to each computer in your network :

    The should be a file containing a server's name or IP on each line. For example:

    Since this is the first time you do this, you will have to manually enter the password for each IP but once you've done that, you will be able to copy files to any of these machines with a simple:

  3. Remove the expect from your script. Now that you have passwordless access, you can use your script as:

expect installation grep Memory` if (... (2 Replies)

Discussion started by: umen

2 Replies