How to setup a node.js development environment on Mac OSX Lion

Install node.js via Homebrew

nodejs-old

There are several ways to setup a node.js development environment on Mac, you can either choose to download and compile it form source or using a package management system to do the works for you. Because node.js is still a pretty young project, it updates frequently. I suggest install node.js via Homebrew. You will find it is much easier not only to install but also update it. If you use Ubuntu you can check out this post for the instruction.


Install Xcode

What? I just want to code some server side javascript why i need to install the 4.3 GB Xcode 4? Well, you need gcc to compile node.js and other packages.

Install Homebrew

Homebrew is my favorite package management system on Mac. It’s like apt-get for Ubuntu. We are going to need it later for installing node.js and mongoDB. If you haven’t heard of it. You should try it now.
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Edit 2011/12/18

I’ve found it’s easier to install node through nvm, a node version management system. It’s something like rvm for ruby. You can switch between node version for development usage. Also npm now come with node since version 0.6.3 which means you don’t have to install it by yourself. So basically you can ignore the Install npm step.

Install node.js

With the following 2 command you are done with installing node.js, Easy!
$ brew update
$ brew install node
The above methods are deprecated. Please use the following commands.
# clone repo
$ git clone git://github.com/creationix/nvm.git ~/.nvm
# enable on terminal open
$ echo ". ~/.nvm/nvm.sh" >> ~/.bashrc
# reopen your terminal and do the following
$ nvm install -s v0.10.3
# set default node
$ nvm alias default v0.10.3

Install npm

npm is the most popular package manager for node. It is like gem for ruby and pear for php. There are thousands of packages available out there including ORM, router, third party api wrapper … etc. So before you write your own solution make sure to check if it is already there on npm.
$ curl http://npmjs.org/install.sh | clean=no sh

Install mongodb

mongoDB is my number one choice NoSQL database for node.js. It is not the fastest but the easiest to use especially for people used to work with relational databases. However please do not design your data structure as what you used to have for a relational database. Otherwise your node.js app will run slow. Please do checkout their awesome docs before you start.
$ brew install mongodb
# create db directory
$ mkdir /usr/local/db

Update node.js

Just install different versions of node like what we did above and switch between them.

Update mongoDB

With Homebrew updating mongoDB is as simple as the following 2 command.
$ brew update
$ brew upgrade

Setup a node.js development environment on other OS

If you want to setup a node.js development environment on Windows or Ubuntu 11.04, you can take a look at:
How to setup a node.js development environment on Windows
How to setup a node.js development environment on Ubuntu 11.04

npm commands and node.js basics

Now you have setup a clean node.js development environment. Let’s have a quick look at how to use npm and learn some basics of javascript and node.js.


Related posts