Tutorial: Get started with Node.js on Mac (Beginner)
May 7, 2012
This is a guide on how to get up and running with Node.js on Mac OS X. The purpose of this tutorial is to be a reference post for other tutorials because when you have done it once, then you will probably not need to do it again. So I feel it's unnecessary to include it all the time. Plus, it's probably very basic for most of you.
However, if it is the first time you do it or need to freshen up your memory, then I hope you will find this helpful.
GOALS
After having gone through this, we will have:
- a working installation of Node.js
- a working installation of Node Package Manage (NPM)
- created a minimalistic webserver instance
LET'S GET STARTED
The first thing we need to do is to install Node.js. Depending on what you have available on your system already, this can be done in a few different ways. I'll go through them one by one.
1. Install using Macports
If you have macports installed then you can simply install node by opening a terminal and enter the command:
$ port install nodejs
Note that since Macports is by default installed in /opt/local so it is often required to prepend the command with sudo
2. Install using Homebrew
If you have homebrew installed then you can similarly install node by opening the terminal and enter the command:
$ brew install node
Unlike macports, homebrew is most often configured in a way that you will not need the sudo
command when installing. However, there are exceptions...
3. Install from package
The third option is to go to nodejs.org/#download and download the Macintosh installer package. Once it is downloaded, open the package and follow the instructions.
Verify installation
Lastly, whatever method you chose to install by, we need to make sure everything went okay with the installation. Open up a terminal and type the following command:
node -v
Which should output something like this:
v0.6.16
As you can see, at the time I'm writing this, the latest version is 0.6.16. But as long as you get any version output then it's successful.
NODE PACKAGE MANAGER
The next step is to install npm which as it's name suggests, handles the packages that you can add to your projects.
Oh wait... this is not like in the early days of node anymore. Npm actually comes bundled with the Node installation as of v0.6.3.
" Then why the hell did you split it up in different steps in your tutorial? "
Well, actually it is only included if you installed node by using the package alternative. Otherwise you will have to install it separately by either of these commands:
port install npm
or
brew install npm
Once this is done we will verify the installation in the same way we did with node. Open up a terminal and type the following command:
npm -v
Which should output something like this:
v1.1.20
CREATE NODE PROJECT
Now that we're done with the configuration and stuff, let us at least do something to prove that it is working. Start with creating a new file called app.js
and add the following lines of code:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('How you doin?\n');
}).listen(3000, '127.0.0.1');
console.log('Server running at http://127.0.0.1:3000/');
And then start the server by running the following command:
node app.js
To verify that it is working, open a browser and go to http://localhost:3000/ which should show you the resulting message "How you doin?"
Good luck and happy coding!