Learn nodejs, Installing npms globally on a PC

Most npm packages will be installed locally. This is because your application itself will be dependent upon these packages, and they will need to accompany your app in runtime. JavaScript is no longer limited to the browser so the npm registry contains many applications that you can install globally.

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.

So I don’t have to stop and restart my app. In fact, I can just go to the browser window and hit refresh and now we can see our change take place. Another good npm that you might want to install globally is JSHint. JSHint is a tool that will inspect your JavaScript code, and notify you of any potential errors. Let’s go ahead and install JSHint globally. So, with the Command Prompt open as the administrator, I’m going to go ahead and stop our Web Server from running.

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.

Now, the second problem that we see on line 6 is it says that backtick is unexpected. And that’s because by default JSHint does not like the ES6 syntax for template strings. We can actually tell JSHint that ES6 is okay by adding a comment to the top of our app.js file. So, let’s go ahead and navigate to our code in Sublime. And now, I’m just going to add a comment for JSHint. I’m going to tell JSHint to use the esnext option. And, this tells JSHint that there is ES6 syntax in this JavaScript file so don’t complain about it.

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.

Leave a Reply

Notify of