- [Instructor] Note, JS comes with a child process module,
which allows you to execute external processes
in your environment.
In other words, you node JS app can run and communicate
with other applications within the environment
that is hosted.
In the next two lessons, we're going to take a look
at the two main functions used to create a child process,
spawn and execute.
So far, we've been running our node files from our terminal.
We're typing into the terminal or
dos command prompt system commands.
I'm working on a Mac so the system commands
I can used are based on unix and work in a unix environment.
Come over here and add a new file.
And this file is going to be called EXEC dot js.
And what were going to do is we're going to take a look at
the child process module.
So let's first include it.
CP will equal require, child underscore process.
So all of the commands that I have just
executed are synchronous.
They run once, spit out some data,
and then the process ends.
So the execute function is for synchronous commands.
I can do CP dot E X E C and then,
using the execute function,
I can open or run any of these commands.
So I can go ahead and open LinkedIn.com/learning.
And we'll save this and we'll open up our terminal.
And now when I run node EXEC we see that we open
the Browser to LinkedIn Learning using the execute function.
I can also open up another instance
of the terminal from my program.
If I were to say open,
dash A terminal to this folder dot,
and save this, and then go ahead and run that.
We execute a command that opens up another
instance of the terminal.
If the command that we're executing returns data,
like a L S command, we can grab that data.
So what I'm going to go ahead and do is execute an LS.
So If there are any errors,
I'm just going to go ahead and throw them.
Otherwise, I'm going to log the data.
SO now, if I clear my terminal
and run our command node execute,
we can actually see a list of the directories
and libraries using the LS command.
What happens when I add an error?
So if I were to change this LS command to LST,
well there is no LST command, so it says LST command
not found if I try to run it in the terminal.
But what happens when I run this node JS application?
So we can see that there's an error,
and we threw that error.
The error's printed right here.
The command that we tried to execute has failed.
That's just the node JS error.
If we wanted to see the error that was returned
from the command when we executed it,
we can see that by looking at the third argument.
Which I entitled stderr.
So let's come back out to the terminal.
So now if I try to run this file, node EXC,
I can see the error that's being returned from bashed
because if I run LST, that's the error that I get.
So the execute command, I can use to execute any
synchronus process.
This includes any of our node processes.
So I can actually run node, readStream,
just like I would in the terminal,
and we can console log the data that gets returned here.
And what you'll notice is this time when I run node execute,
it's actually running another node JS process, readstream.
And we actually get to see the data that was returned
from that process, right here in the terminal.
We were able to do all of this with the execute function
and the child process module.
In the next lesson, we're going to take a look a spawning
child processes and how that's different
than executing them.