NPM is the Node Package Manager and we need to learn how to use this before we do anything else because this is what brings to us the power contained in the packages that house the command line tools. When you installed Node (like I taught in the previous post about Node), you automatically get NPM as well. So first we need to fire up our console/shell.
Every command that utilizes NPM starts with
npm. If you just type in
npm, then it’ll give you a good long list of commands that you can use with it, but there’s really only one command that we care about right now, and that one is
install. You can read the online documentation for install if you want.
To install a package for use, you can simply write
npm install PACKAGENAME. If you want a specific version of the package you can write it like this:
npm install [email protected]. This will install the package in a node_modules folder in the current directory, which will make the packages and modules available to use in your applications via
require. When it comes to using the command line tools, though, you want them to be installed globally so you only have to install it once and it’ll work for every project and you aren’t required to be in a specific folder to use them. To install a package globally, use the
-g flag like this:
npm install –g PACKAGENAME.
There is one other common way to install packages. If you’re doing a project that depends on several packages and you want to keep it in a remote repository, then you can actually skip pushing the package files up to the repository and just keep all the dependency data in a file named package.json. This is a simple JSON file and one of the properties of the main object within it is
dependencies property lists all the package names and versions of packages that your project needs. You can check out this package.json file for an example (the dependencies property is near the bottom). If you are in a directory that includes a package.json file, all you need to type into the command line is
node install and it will run through all of those dependencies and install them for you.
Now you’re thinking “cool, but I don’t know what any of the packages are.” Not to worry. There is a website where you can search and find packages: the NPM Registry. Right on the front page it shows the packages that have been updated most recently and the packages that are used most often. There’s also a little search bar you can use to try to find packages using keywords or package names. Search for “MVC” for example. This will bring up a list of numerous mvc frameworks. The text in red, that is also a link to more information about the package, is the name of the package that you will use when you try to install it. The names appear to be case-sensitive, too.
There are many awesome command line tools like jshint, and uglifyjs, which will lint your code and minify it, respectively, all from the command line. No more need to go to the internet to copy/paste the code/url into an online tool. You’re already using Node, so why not expand out a bit and use command-line tools built on it instead?
You can also find a bunch of grunt extensions if you do a search on the registry for “gruntplugin”. Personally, I’ve gotten to liking bbb (Backbone Boilerplate Build Tool). Install it with
npm install –g bbb. It sits on top of grunt so everything you would normally do with grunt can now be prefixed with
bbb instead. The Backbone Boilerplate Build Tool’s main function is creating scaffolding and pulling in dependencies for creating Backbone.js applications. This project is where I got the main concepts for the config.js file in the final video of the Backbone.js App Walkthrough series. Until Yeoman comes out, I think this’ll be my best friend.
There are tons of other great command line tools and plenty more to learn about NPM, but I hope that I’ve whetted your appetite and you will start experimenting and clicking through all of those links I threw into the post. Also, look forward to a bit more on Node in upcoming posts. I’m still not 100% sure what I’ll be writing about in them, so if there are some Node-specific topics you want me to cover, let me know in the comments below. God bless and happy coding!