So, from the npm registry, node-dev is one of the packages that we are going to be installing globally during this lesson. Node-dev is a tool that we can use while developing Node apps. We can run our code with node-dev, and it will automatically restart our app if we make any changes to the source code.
Let’s go ahead and install node-dev globally. Now, to do a global install on a PC, you need to have administrative privileges. So, I’m going to go the start menu and search for the Command Prompt. But, it if I want to do a global install, I’m going to need to run the Command Prompt as the administrator. With administrative privileges, you can do a global install. Whenever you see me do a global install on a Mac, I use sudo. That stands for super user do or basically, run that installation as the administrator.
So you will not need to add a sudo command when you do a global install but you will need to make sure that the command prompt is open as the administrator. I’m going to navigate to my Exercise Files. And, in the start folder for Chapter 7, Lesson 3, we have a simple HTTP Hello World server app. Now, typically we would run this with Node. And, that’s exactly how we would want to run this app in production. But while I’m working on the app, I want to use node-dev. So the first thing I’m going to do is install node-dev.
Npm install -g node-dev will globally install node-dev. It’s the -g that is the global flag. And now node-dev will be installed and added to our path so that we can use node-dev across projects. So, now I’m going to ahead and run our app with node-dev. Node-dev app.js, and doing so, we see that we have a Web Server running locally at localhost:3000. So, I will go over to the browser window and navigate to localhost port 3000, and we can see that our Web Server says Hello World.
Now, let’s say I want to change these files. So, let’s go ahead and open up our Exercise Files. And I’m going to open up the app.js files in Sublime. And, instead of saying Hello World, what I’m going to go ahead and say is, HELLO HELLO HELLO. And when I save this file, node-dev is going to pick up that save and automatically restart my app for me. If I navigate to the Command Prompt, we can see that node-dev has noticed a change with my app.js and it automatically restarted it for me.
And I can enter a CLS to clear the screen in our Command Prompt. And now I’m going to npm install -g jshint. And, as the administrator, I have installed JSHint globally so now we can use JSHint to hint our code across applications. Let’s go ahead and run JSHint on our app.js file. And, doing so, JSHint tells me that we have several problems here.
Let’s go ahead and navigate back to our Command Prompt and run JSHint again on our app.js file. And this time, you can see that it is not complaining any more about those backticks or those template strings but it has found that we are missing semicolons on line 3 and on line 18. So I can go back to my app.js file and add semicolons on line 3 and it looks like I forgot one down here at line 18 as well. So I can add those and save this file. And then, we can go back to our Command Prompt and run JSHint one more time.
And in doing so, we don’t see any output which means that JSHint has run successfully and it is okay with all of the syntax in my app.js file. So, this is how you can use npm to install packages globally. The packages that you will typically install globally are applications that help you improve your web development process. These include tools for running tests, building your app, documenting your code, deploying scaffolding, hosting node clusters, just about anything you can imagine.