Installing Node and npm

December 10, 2010 - by isaacschlueter

In preparation for Node Camp I thought it would be a good idea to help any of you new noders out there get started with the newest, hottest piece of awesome to hit the interwebz since HTTP.

These instructions will take you from a computer to a computer with node and npm (which I think we can all agree is a huge improvement.)

Note: If you’re working on an no.de machine, then you should be aware that node and npm are already installed, so you can skip straight to the part where we all party and they roll credits.

When Things Go Not Good

There is a lot of help available to you. The #node.js room on irc.freenode.net usually has a lot of people willing to help out with almost anything (or bicker about pointless JavaScript style discussions), and the NodeJS mailing list is very active.

So, if you get stuck, don’t fret. We’re essentially friendly, if a bit nerdy.

Step 1: A Posix OS

If you’re not on Windows, congratulations! Proceed to Step 2.

If you are on Windows, go get Cygwin and install it. The instructions are available on the node wiki

Important: Make sure you follow ALL the steps! It’ll seem like it’s working before you get to the end. But it is a dirty, lying liar. Don’t believe it. Do the steps all the way to the end. If you don’t, you’re going to complain that npm says “ECONNREFUSED” and then post a bug, and I’ll ask if you did all the steps, and you’ll feel silly.

Step 2: A Compiler

If you are using almost any kind of Linux or Unix system, you should be golden at this point. Just skip ahead to step 3.

Mac users, go and get Xcode, if you haven’t already. (Protip: It’s on the OS X install CD if you don’t wait to wait for the giant download.)

Step 3: Make Friends with the Terminal

By this point, you’ve probably had to enter some commands into the terminal at some point in your setup process. That’s good.

For Mac users, I highly recommend iTerm 2. It’s a recent fork of the otherwise abandoned iTerm project, with regular updates and bug fixes and a sane preference menu. Love it.

From here on out, I’m gonna expect that when I say “enter this command:” you’re gonna know how to do that.

Just to get started, let’s make sure that you have a compiler. In your terminal, enter this command:

which gcc 

It should print out something like /usr/bin/gcc. That’s how you know it’s working.

Step 4: Install Node and npm

I’ve had to answer this question a lot, so I put together a few different ways that it can be installed. You can find them here in this gist.

There is not a single best way to install node or npm. However, in my personal experience, using a package manager like Homebrew or Aptitude is a recipe for disaster. These projects are moving very quickly, and are still very young as of the time of writing this.

Each of the setups in the gist will get you a setup with node and npm commands in your path, without having to run anything as root. Both programs are very configurable, which is great in some ways, but also can be intimidating. If you're not very opinionated about this sort of thing, you can safely just take the first batch of commands, run them, and it should pretty much Just Work.

Personally, since I like to do development on node and npm, I have the git repositories checked out into a folder in my home directory where all my projects live. To avoid having to sudo everything, I used the “take ownership” technique. When I want to check out how something works on a different version of node than the bleeding edge, I use a nave subshell.

If you’re not hacking on node and npm all the time, and just using them to build some other thing, then you’re probably better off using one of the other methods described.

A Word About Version Managers

Nave is a neat little tool. You tell it which version you want to use, and it drops you into a little subshell where that version of node is “the” version. You can also use shorthands like “latest” and “stable”. If you use npm inside those subshells, then the npm packages you install will (by default) just live in that ecosystem of subfolders, safely segregated from other versions.

NVM is another tool which serves a similar purpose. However, instead of dropping you into a subshell, nvm sets environment variables directly in the current shell. The installation is a bit different, because you have to add some stuff to your .bashrc file. But it also lets you specify a version, and npm packages will be segregated by default.

Other than their difference in implementation, nvm and nave are very similar in how they function. It’s not a complex problem, really. Download some stuff, build it, make it the first thing in the PATH. They were written around the same time, largely due to differences in Tim’s and my esthetic preferences. They’ll both probably keep working indefinitely, since there’s not much to either.

Step 5: Go Build Something!

You can now install node programs (including your own!) using npm. Type npm help or npm faq to get started.

To run a node program, write out some stuff into a js file, and then do: node my-program.js. Node has a lot of cool stuff built in, and if you run npm ls latest, you'll see there are a ton of userland modules to choose from as well.

So go make something awesome, and use this new power for Good!

Screenshot from jsapp.us, in case you're too lazy or impatient for the install.

:

Sign up now for Instant Cloud Access Get Started