Node is a powerful tool on it's own, but it's real strength comes from the huge community that surrounds it. People are constantly developing new tools and libraries to make developing for node even easier. We're going to quickly go over npm
, the program that lets you tap into these resources.
###What is npm?
npm
is a program that allows you to easily install libraries or tools made by other node developers. It's installed automatically when you download node, so if you have node then you should already be set up to use npm.
Here's how it works. First, other developers will upload a module
they created onto the npm website. A module is just a reusable chunk of code created to do a specific thing. Lets say we want to install this cow module for some reason. All we need to do is open the command line in the same folder as our project and run:
#This will install the "cows" module into the current folder
npm install cows
It's really that easy! npm will download the module into a folder called node_modules
. Once the download finishes, you can load it up using require()
like we showed earlier. When loading modules with the require()
method, you just use the name of the module (don't include the .js
). Here's an example:
//Load the cows module into the 'cowsModule' variable
var cowsModule = require('cows');
//Run the cows module, which just returns us some cow text
var myCowsArray = cowsModule();
//Print the 1st cow in the array so we can see the result
console.log(myCowsArray[0]);
/**We see a beautiful cow generated by the cows module
(__)
(oo)
/-------\/
/ | ||
* ||----||
~~ ~~
Cow
*//
Of course, this is just an example. Most modules on npm are a little more useful than this one. But key here is that you can go from seeing a module you having it installed and running within a few seconds.
To make things easier, it's important to keep track of which things you've installed with npm so far. For example, if multiple people are working on a single project, you all need to keep a list of which versions of which modules are being used. Luckily, npm can be told to track this automatically. Here's now:
First, you run the npm init
command to initialize npm. It will ask for some information about the project, but you can just go with the defaults. npm will save this information inside a file called package.json
.
Next, whenever you install a package, just add the --save
argument to let node know that you'd like to track this package for later. The module you installed will be added to the package.json
file automatically.
npm init
npm install cows --save
npm install someOtherModule --save
npm install someThirdModule --save
If another developer sends you their code, they normally will not include the node_modules
folder. Luckily, as long as you have a package.json
file, it's easy to quickly install anything you need. Running npm install
without any arguments will automatically install everything listed in the file.
#This would install all modules listed in package.json
npm install
There's honestly a module for almost anything. If you can imagine it, it probably exists. As of right now, there's almost 500,000 modules uploaded on npm. Here's some of the most popular ones:
- Express is an incredibly popular web framework. We'll be covering this later.
- Request is a module that makes sending HTTP requests to other servers pretty easy
- NodeMon is a tool that restarts your node project automatically when changes are made. This can save you a lot of time!
- Gulp is a task-runner tool. It's useful for when you want to set up some tasks to run on your code (for example, maybe you want to spellcheck your files every time you save them).