//

INSTALLING NODE.JS

In this article we are discussing how to get Node.js working on OSX along with npm, the package manager for node.

There are two ways of installing Node.js on Mac Download package from nodejs.org or download to one click install ing of Node.js and NPM.

Install from source

To install from source you probably know what you are doing but this article might help.

Install Homebrew

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Install Node.js via Homebrew

Once Homebrew is installed you can go ahead and install Node.js

brew install node

Now create a file called server.js and paste the server code

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');}).listen(3000, "127.0.0.1");
  console.log('Server running at http://127.0.0.1:3000/');
}

Save the file and from the console run.

node server.js

Now you can visit http://127.0.0.1:3000/ with your favorite browser and you are up and running with server side JavaScript. Further more you can read the Node.js documentation if you want to learn more about what Node.js is and what you can do with it.

Installing npm

Node.js provides low level, lightweight platform, so there are lot of modules created for Node. The  package manager for Node - npm will help you manage these.

Even though the package manager was recently removed from Homebrew, you'll need to install it manually.

curl https://npmjs.org/install.sh | sh

If executing scripts from curl doesn't sound good for you, you can download the source and then install:

git clone http://github.com/isaacs/npm.git
cd npmsudo make install

You will find out that everything worked fine, other than having to set NODE_PATH. NODE_PATH tells Node.js where to look for modules. This means you can do things like var client = require 'http'instead of specifying the full path.

To make sure my shell knows where to find Node.js modules, I added export NODE_PATH="/usr/local/lib/node" to my .bashrc file.

As some modules have executables you will also need to add /usr/local/share/npm/bin to your PATH. Here's how it might look export PATH="/usr/local/bin:/.../usr/local/share/npm/bin:$PATH"

Make sure you reload your shell with source ~/.bashrc so the changes are applied.

Installing modules

Now we are can install Node modules using npm. Let's start with Express, which is a good place to start - a Node framework inspired by Sinatra. npm install express

This provides a solid foundation to start developing with Node.js including Jade Node template - the Haml (HTML abstraction markup language)  inspired Node templating engine. Now let's code!