New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Maintaining projects using the npm command

From: Node.js Essential Training

Video: Maintaining projects using the npm command

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.

Maintaining projects using the npm command

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.

Show transcript

This video is part of

Image for Node.js Essential Training
Node.js Essential Training

52 video lessons · 11774 viewers

Joseph LeBlanc
Author

 
Expand all | Collapse all
  1. 3m 9s
    1. Welcome
      42s
    2. What you should know before watching this course
      57s
    3. Using the exercise files
      47s
    4. Using the challenges
      43s
  2. 18m 50s
    1. Why use Node.js?
      2m 1s
    2. Choosing a Node.js installation process
      4m 56s
    3. Installing Node.js on the Mac
      2m 5s
    4. Installing Node.js on Windows
      1m 43s
    5. Installing Node.js via Node Version Manager on Ubuntu Linux
      4m 45s
    6. Installing Node.js via Node Version Manager on a Mac
      3m 20s
  3. 13m 54s
    1. Exploring language additions to the V8 JavaScript engine
      3m 38s
    2. Understanding require() and modules
      6m 39s
    3. Understanding callback execution in the event loop
      3m 37s
  4. 12m 23s
    1. Initializing Node.js projects
      2m 37s
    2. Finding an NPM
      3m 30s
    3. Maintaining projects using the npm command
      6m 16s
  5. 21m 4s
    1. Creating modules with getters and setters
      6m 33s
    2. Understanding module caching and scopes
      6m 15s
    3. Implementing JavaScript creational patterns
      5m 8s
    4. Challenge: Leveraging module caching
      55s
    5. Solution: Leveraging module caching
      2m 13s
  6. 32m 27s
    1. Starting Express applications
      3m 42s
    2. Handling GET requests returning JSON
      3m 26s
    3. Navigating large datasets using request variables in routes
      5m 4s
    4. Modifying data through PUT requests
      4m 32s
    5. Supplying middleware to modify HTTP response headers
      4m 37s
    6. Generating HTML views in Jade
      8m 9s
    7. Challenge: List all records as JSON
      37s
    8. Solution: List all records as JSON
      2m 20s
  7. 25m 37s
    1. Evaluating unit testing methodologies
      3m 8s
    2. Isolating existing code for testing
      6m 47s
    3. Supplying helpers for test data
      2m 35s
    4. Writing unit tests with Mocha
      6m 5s
    5. Simulating HTTP requests with SuperTest
      7m 2s
  8. 28m 45s
    1. Connecting to MongoDB via Mongoose
      4m 16s
    2. Defining schemas for collections in Mongoose
      3m 47s
    3. Querying MongoDB collections
      5m 32s
    4. Persisting Express sessions through MongoDB and mongo-connect
      7m 6s
    5. Logging in to Express applications using Passport
      8m 4s
  9. 21m 47s
    1. Controlling the built-in REPL
      4m 18s
    2. Constructing a custom REPL
      5m 40s
    3. Receiving command-line arguments with Optimist
      6m 14s
    4. Building command-line tools
      3m 35s
    5. Challenge: Launch a custom REPL from a command
      41s
    6. Solution: Launch a custom REPL from a command
      1m 19s
  10. 15m 46s
    1. Emitting events and attaching listeners
      5m 32s
    2. Streaming chunked data through readable streams
      2m 33s
    3. Controlling readable streams
      1m 52s
    4. Piping readable data into writable streams
      2m 10s
    5. Handling duplex streams over TCP
      3m 39s
  11. 4m 6s
    1. Deploying projects to cloud hosting services
      2m 36s
    2. Finding Node.js resources
      1m 30s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.