Justin Chmura
Blarg

Beginner Node.js – Part 3: NPM

By on

In the third installment of my beginner Node.js series, we will discuss the Node Package Manager, or NPM for short. If you missed the first two parts, make sure to go back and give them a quick read (part 1, part 2).

If you are familiar with other languages like Ruby or .Net, they come with a form of pulling in contained functionality through package managers. Ruby has its gems, .Net has Nuget, and Python has pip. Even operating systems now have their own package managers with OSX having having its own.

NPM LogoNode’s is aptly named, Node Package Manager. We will go over the basics and hopefully become more familiar with working with it as it will become a crucial component of creating apps in Node. As of this post, there are currently over 100,000 packages in the NPM repository that are at your fingertips.



Basics

As part of installing Node, the NPM CLI is also installed. Just to make sure it’s installed, if you type in npm -v into either the console or Terminal, you should see a version number. To get a full list of what NPM commands are available, just type in npm . There’s a bunch of commands and options.

NPM

NPM uses a manifest file called package.json that sits in your project directory. This stores some meta information about the project, but also stores the list of dependencies that the project requires. Every Node package also has its own package.json that lists its dependencies, and so on. If you want to know more about this file, there’s a cool interactive guide that talks about the different properties.

NPM Commands

There’s three in particular that I want to touch on as those will probably be the ones most used during development.

The first is npm install <packagename> . As you can probably decipher, this is the command to install a particular package. This will install the package to whatever directory you are executing the command from. It creates a node_modules folder where it stores all the files for each package. If you add the &#8211;save flag to the command, it will add the package as a dependency to the package.json .

The second command is npm uninstall <packagename> . I don’t think I need to go into much detail with this one other than it removes a package that you have installed. If you run this command with the &#8211;save flag, it will also remove the entry from the package.json .

The last is npm init . This is used to bootstrap a new Node project. This provides a quick wizard in creating a new, bare-bones package.json . It doesn’t do anything relating to packages, but it at least gets you off the ground pretty quickly if you don’t know what should go in that file.

Global Packages

NPM does give you the ability to install packages globally. Packages that support this are mostly command line utilities that are written in Node. In order to install a package globally, add the -g flag to the npm install command. You can run this from any directory. It installs the package into its own folder elsewhere on the machine that when installing Node, should be added to the list of places to look for commands.

Let’s say you wanted to run JSHint from your command line on a particular file. There’s an NPM package that gives you that ability. To install it, run: npm install jshint -g . Now you can run jshint myfile.js and lint any JavaScript files right in the terminal. That kind of flexibility allows you to use some neat utilities that other people wrote.

Part 4, The Fun Begins…

Now that you have been exposed to the world of Node packages, the possibilities of what you can do with Node are endless. There’s a package for almost any situation and more are being added all the time. I recommend poking around the NPM repository and looking at the more popular libraries. Maybe play with a few, like ExpressJS, which we will be using later on in this series.

In part 4, we will start our first project using NPM with the commands listed above and discuss structure of a Node app.