In this video you're going to learn how to install and work with global NPM packages.
These are going to allow us to get a new command we can execute from the terminal.
So far all of the packages we've worked with are known as locally install the packages.
That's when we install the dependency explicitly into our project like we've done with validator and
chuck in the notes app project.
You can tell it's a local dependency because it's listed in package.
Jason like our two dependencies are now local NPM modules those get loaded into your project code with
require like we've seen with chalk and validator when we install a module globally we actually don't
load it indirectly to our source files instead we install it globally and it gives us access to a new
command we can use from the terminal.
Now in the case of this video we're going to install a NPM module globally that is a really nice utility
for working with node because it's going to allow us to run our application and automatically restart
the app whenever the app code changes.
So I can make a change up above and save the file and will immediately see the results down below.
So we won't have to constantly switch to the terminal and rerun the same command over and over again
we can find the package we're going to install by heading over to NPM and J ask.com and going up to
that search bar once again right here.
The package we're gonna use is called a Node mine and is n o d e m o n.
Right here the first result is the package we're looking for and I'm just showing you where this package
lives in case you want to learn more about it.
It's actually a very easy tool to use.
So let's just get right to it.
The first thing we have to do is install it now when we're installing a module globally the Installation
Command is basically the same with one slight difference.
So let's go ahead and get started down below.
We are going to run a command npm install the module name that's node man at one point eighteen point
five and this is the exact format you'd expect if we were to install the module locally.
The only difference is we use the G flag which is short for global.
This is going to install the module globally instead.
Now let's go ahead and run this command and see what happens when the command finishes.
We're gonna notice that node modules package lock Jason and package Jason haven't changed in any way
what so ever when we install a module globally it is not changing the individual project it's installing
the tool on our operating system itself.
So right here it is going through the process of getting everything set up.
Now we're done.
And if I open up package.
Jason what don't we see we don't see node man listed as a dependency.
So it's not something we would ever require in our project and then actually try to use this particular
package wasn't designed to be used like that.
Instead what we got access to was a brand new command from the terminal.
Now when you install an NPM package globally things can indeed go wrong.
I usually get a ton of questions related to this since everyone's environment is slightly different.
If you are running on Linux or Mac and you're getting an error I recommend putting sudo that's S U D
O in front of the command to run it as an administrator.
So that would be pseudo npm install node man at one point eighteen point five with the G flag if you're
on Windows and you're getting some sort of error just crack open a question and of course if you want
a and I'll work with you to get unstuck and back on track we can always verify that node man was installed
correctly by running node man the package name exactly followed by the V flag exactly like we've done
with node and with NPM when I run it what do I get I'll get a version one point eighteen point five
the exact one I specified in my installation command this is Andrew from the future with a quick update
if you're running on Windows and you're using power shell as your terminal the command that we just
ran to check the node man version might have failed so you might be seeing the error.
That's right up top when I run.
No demon with the V flag I get the following error saying that no demand can not be loaded because running
scripts is disabled on this system so if you're running into that don't worry.
There are a couple of fixes.
The first thing you could do is use the regular command prompt application instead of using power shout
if you'd like to continue using power shell.
You can work through the four steps down below.
So step one click on the windows icon and search for power shell.
Step Two right click power shell in these search results and click Run as Administrator.
Once you have it running as an administrator in step three run set execution policy setting and equal
to unrestricted spacing and the capitalization is important.
Once you run that command you should be able to move on to Step Four and to rerun no demand with the
VA flag and instead of getting the error up above you should indeed get the version installed.
So this is a fix for Windows users using power shell.
All right let's jump right back in to the lesson if you're seeing a version that's great if you're not.
That means once again something went wrong and you can always crack open a question in the Q and A and
we'll work with you to get new global modules working correctly.
So we have access to this new node man command and the question is how exactly do we use it.
Well let's explore that.
First up I am going to clear the terminal output to make things a bit easier to read and all we do is
we run our script like we would have with node but we change the command we're using.
So in the past we use node space at DOT J S to run the script once with node man.
We use node Mun space app dot J S so the only thing we've changed is we've swapped out node for node
man.
Now if we run it we're gonna see our script run with a few additional pieces of output.
Right here we can see it starting node man.
And it lists the version as well as some other logs saying that things are getting started.
Then we do indeed see these two lines that come from our script down below we can see that the program
has exited and it's waiting for changes before it restarts.
So now node mine is up and running and you'll notice it's not bringing us back to the command prompt
even though our program has finished the node mind process is still executing and if we change something
up above we'll see that change instantly printed below when we save the file.
So I'm gonna change chalk dot green over to chalk dot read and then I'm gonna change the message from
success to error.
If I go ahead and save the program down below I can see that basically instantly node mine restarted
the script and now we see the new output from our program.
We're still getting the your notes message and now we're seeing our error shown in red.
We could continue to make changes to our program and see those changes instantly reflected.
I'll switch this to blue save the program and before I'm even done saying I saved it Blue is showing
up down below.
So throughout this class I will be using Node mine and I also use it with my client projects.
So we don't have to constantly restart the node script every single time we make a small change.
It is a great utility just to improve your overall developer experience.
Now if you want to stop node man you can do that by going to the terminal and using control see that's
going to terminate the node and process and bring us back to the command prompt where we can do something
else like install a new module maybe run the script once with node or maybe restart it with node mine
in the future.
All right that's where we're going to stop for this video and that is also the end of this section on
the Node modules system.
You now know how to load in core node modules how to load in your own files and how to load in NPM modules.
This is gonna give us a foundation for the rest of the class where we're gonna do those things over
and over again in the next section.
We're gonna start to explore command line arguments and the file system and we're actually going to
work to build out that notes application.
I am excited to get to it.
So let's go ahead and jump right in into the section introduction for the next one.