Installing Node, NPM and Express on OSX from Scratch
Apr 01, 2011
You need to have Git installed. By far, the easiest way to do this is with Homebrew. Follow these Homebrew Installation Instructions.
Now that you’ve got Homebrew installed, use it to install Git from within your Terminal:
If you don’t have Git installed, use Homebrew to install that first.
Here’s a basic installation guide for Node.
Getting Node’s source code
If you don’t already have a place to store external source code, put it somewhere usefuls, like a “src” directory in your home folder:
Now that we’ve got a place to put it, let’s download Node:
$ cd ~/src
$ git clone https://github.com/joyent/node.git
$ cd node
You’ll want the latest stable version. Stable version are use even numbers, while unstable dev versions use odd. At the time of this writing, the latest stable release is 0.4.7, so let’s checkout that tag before building:
$ git checkout v0.4.7 # you'll get a note about a detached HEAD state.
$ export JOBS=2 # optional, sets number of parallel commands.
$ mkdir ~/local
$ ./configure --prefix=$HOME/local/node
$ make install
Telling your system where to find Node
We’ve got to add the node bin folder to your path so that you can easily use node from the command line. Open up your
.bash_profile and add Node’s path to your PATH.
If you already have a line that starts with
export PATH=, just add Node’s path to the beginning. For example this:
If you don’t already have a PATH export declared, just add this to the file and save:
Testing your Node installation
To make sure this all worked as expected, open up a new Terminal window and run:
Now, I’m guessing you’d like to install some Node programs, like Express or something like that. EASY! NPM is incredibly easy to install:
$ curl http://npmjs.org/install.sh | sh
This should print out a bunch of stuff, ending in
It worked. There may also be a single
npm WARN Not installed in blah blah blah... line – don’t worry about it.
Testing your NPM installation
Again, just open a new Terminal window at run:
Once incredible helpful feature of NPM is that implements Bash completion, which makes using NPM quite a bit easier at times. To enable this you’ve got to source NPM’s completion file into your .bash_profile. Open .bash_profile up again, and add this to it:
Now that we’ve got NPM installed, we can use that to install all the Node.js goodness we can get our grubby little hands on, including Express!
NPM 1.0 is more more opinionated than earlier releases – instead of installing modules globally by default, modules are installed at the project-level. Command-line tools, on the other hand, can be installed globally.
Express is both a project module, and a command line tool, so we’re going to install it in both places. Let’s start by installing it globally:
Now that we’ve got our
express command-line tool, let’s use that to create an Express application:
$ mkdir ~/Sites/bitchin-express-app
$ cd ~/Sites/bitchin-express-app
$ express -s -t jade
Next we need to specify our application’s dependencies. NPM gives us a clean and maintainable way to do this with the “package.json” file. Create this by running
npm init from with the root of your project, and just follow the prompts:
When you’re done with that, add your dependency declarations to the file. Express and Jade are both dependencies for me. This is what my final “package.json” looks like:
"description": "My Bitchin' Express App",
"author": "Dan Dean <email@example.com> (http://dandean.com)",
"node": ">= 0.4.7 < 0.5.0"
"express": ">= 2.0.0 < 3.0.0",
Dependency management is one of NPM’s excellent features. We can automatically install all of our application dependencies by simply asking NPM to take care of it. From within your project folder run:
You should notice that your project now has a “node_modules” folder. Inside that you’ll see an “express” folder, which contains the Express framework module for use just with your new application.
Running Your Application
We’ve got to create a couple more little files before we’re ready to go: our “layout” and “index” files.
$ touch views/layout.jade views/index.jade
You’ll notice that back when we created our application we passed the
-t jade argument to
express. This configured our app to use the Jade templating engine.
Open up “views/layout.jade” and paste this into it:
title Hell's Yeah.
And paste this into “views/index.jade”:
h1 This is a Bitchin' Web Applications!
p Am I right?
Now all we have to do is start our fucking rad, Bitchin’ application! From the root of your project run this, the open the
http://localhost:3000 in a browser:
$ node app.js
> Express server listening on port 3000
Hopefully this introduction is enough to get you up and running. There’s a whole lot going on in the community. Keep up to date by following the Node blog. If you want to explore the many modules available for use in your applications, poke around the NPM Module Registry.