Getting Started with Node.js on Mac OS X

This is a step-by-step process on installing Node.js on Mac OS X. There are several “getting started” tutorials online but I didn’t find much on Mac OS X. The ones I did find were a little confusing since I had no previous experience with Node.js. At the time of this tutorial, I’m running OS X 10.8.2 also known as Mountain Lion.

Installation
There are several different installation instructions using Homebrew and other methods but the easiest is to install the Mac Installer Package straight from Node.js. Once it is downloaded, open the package and follow the instructions. The default installation path is /usr/local/bin/node but you can change that to the path of your choosing. The great thing about the Mac Installer Package is that it also installs Node Package Manager (npm) which, in the past, used to be a separate install. The default installation path for npm is /usr/local/bin/npm.

Install Verification
To verify that Node.js was installed successfully, open up terminal and execute the following:

node -v

The output should look like v0.10.0 or whatever version of node is current at the time you install it. The same thing can be done to verify npm.

npm -v

Creating you first Node Project
Now you can create you first node project and confirm that node is working correctly. You can use the node sample project provided on the Node.js homepage. Create a new file called example.js and add the following code:

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

You can start the server by running the following command:

node example.js

Note: example.js must be saved under your user directory. If not, then you need to provide the entire path or change it using cd.
To verify that its working, open a browser and go to http://localhost:1337/. You should see a “Hello World” message.

Kill the server
To kill the server enter Ctrl-C (Ctrl + C) in terminal.

Thats all there is to it. You have successfully installed Node.js and ran you first node project. I have to admit after reading several online tutorials, I thought installing Node.js and creating my first project was going to be harder than this. The Installer Package really makes this an effortless process. Stay tuned for more Node.js tutorials.



10 thoughts on “Getting Started with Node.js on Mac OS X

  1. Pingback: Getting Started with Node.js on Mac OS X | Mode...

  2. There seems to be missing instructions required to enable global installation of npm modules (as opposed to having them locally installed in ./node_modules

Leave a Reply

Your email address will not be published. Required fields are marked *

*

     

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>