Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Although it started out as just a packet manager for node, npm is now capable of installing, starting and maintaining your node project. In this video, we'll take a look at some of the more common npm commands. First let's go to the terminal. I'm going to change direct query to the desktop. Type in CD space and then desktop, with a capital D. Now I'm going to install Express as a global module. To do that, type npm install -g and then space express.
So you'll notice that this failed miserably. It's telling us that the access to the file system is not allowed. Now this may have gone off without a hitch on your computer, but on mine I'm going to need special permission to move forward. So to do that, I'm going to preface this command with sudo. . So I'm going to type in sudo npm install, then -g express. Now the express module has been installed as a global module. Now this is something that confused me a little bit when I approached node for the first time.
I was used to other environments where you always installed packages globally. With node, global modules are almost always used to install command line tools, rather then global copies of packages. The philosophy in node is to let each project define their own required versions of modules, and have them downloaded separately. While this does take up more disk space Most node modules are not all that large, so the extra storage requirement is typically trivial. In this case, installing express as a global module did install a command.
It installed the express command. So, now I can type express and I can type in the name of a project that I want to create. So in this case let's do airline again. So now what's done is it created a folder called airline and it's filled it with some sample code. Notice that one of the files created was package.json. This time express created the package.json file instead of npm in it. On this line, it's telling us to change directory into airline, and then use npm install to get the dependencies.
It's doing this as a one line command, but I'm going to do it as two. So first I'm going to change directory to airline, and now before I type npm install, let's have a look at that package.json file. Open up that airline folder and then open package.json in sublime text You'll notice we have two dependencies. The first dependency is express, and then the second dependency is jade. Jade is a templating engine that works with express. Now, let's list the contents of the directory. You'll notice that there's no node modules folder.
That folder's going to get created when we install the modules. So now let's type npm install. So now the folder has a node modules folder. And inside that folder, is one for express and one for jade. Those were the two dependencies that were defined in package.json. Now let's start the application. Earlier, it told us to run node app to start the application. However, there's a different way that we can start the application. We can actually use npm back here in package.json.
There's a script section starting on line five. On line six we have a start property. The value of that property is the command to use to start the application. Because this is defined, we can just type npm Start regardless of what this value is, then npm will start the application for us, using this command. So let's do that now. I'm going to type npm start, and it's now started the server. So let's go to the web browser and open up local host at port 3000. And it now gives us a welcome to Express screen.
I'm going to close this browser window, and I'm going to stop the server by pressing Control C. Now, let's say for some reason I really didn't want to use Jade. I might prefer a different templating engine, and Jade just doesn't suit my tastes. Now one way we could do this would be to go into Node modules And just remove the Jade folder and it would be gone. But there's a problem with this. And the problem is that we still haven't listed here on line 10, in package.json. So if we were to give this project to someone else and they were to install the node modules, Jade would still get installed, even though it's not being used.
So first, let's remove line 10 entirely, and then also remove that trailing comma, so that the Json file parses correctly. So, let's save this file now. Now, of course, the Jade folder is still going to be there. At this point, we can just remove the Jade folder, and it would be fine. But consider a scenario where you may be cleaning up several modules at once, and making a lot of changes to that package.json file. In this case, you can use npm prune. Npm prune Is going to go through and remove any modules that your project isn't using.
So in this case, after running npm prune, it removed the jade folder. Another handy npm command is npm update. Let's go back to package.json and add jade back again. This wild card means install the very latest copy of Jade. Now as you add dependencies to your project, you can type npm update to get those. Another thing that npm update is going to do, is it's going to automatically update all of the modules that are in your project to the highest acceptable level.
So in this case, with the wild card, it's going to automatically install the very latest version of Jade. But for Express, it's going to always install version 3.4.0. Whoops! It looks like there's a problem here. I tried to update my packages, but I haven't installed Jade again. So to do that let's do npm install. This has now installed both Jade and Express to the latest acceptable version. Now if a new version of Jade comes out and we want to use it, we can type npm update and it'll get it.
Regardless of whether or not there's an update, npm is going to go through and check the modules and make sure they're up to date. Npm has everything necessary for working with community-provided modules, as well as managing your own Node project. In the next chapter, we'll take a look at authoring our own modules.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97270 Viewers
61 Video lessons · 84542 Viewers
71 Video lessons · 68741 Viewers
56 Video lessons · 101233 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.