MacOS X setup for Node.js development - Installing Node

; Date: Mon Dec 09 2013

Tags: Mac OS X »»»» Node.js

I'm setting up a new Mac, rather, I'm doing a clean install of my Mac Mini as a way to upgrade to Mavericks.  I've had the Mini since 2009 and the disc has some cruft so I thought it would be best to start with a clean slate.  That means reinstalling EVERYTHING including all the stuff I use for Node.js development. Hence, the idea came to mind that this would be a useful starting point for a series of blog posts.  I've always wondered what it would take to set up the Node.js equivalent to the MAMP application.

Other blog posts in this series:

For years MAMP has been the go-to application to help with PHP/MySQL development on Mac OS X.  It's simple, just download MAMP and it launches Apache, MySQL, PHP, etc for you, easy peasey.  It gives you a nice control panel to start/stop/configure the processes so you can easily run your application in different environments.

But rather than develop an application/package to manage it, what I'm thinking is it'll be just as useful to write up instructions on what to do.  Rather than rope people into using a special application, why not just show how to set up the Node.JS / Apache / nginx / MySQL / etc stuff on a Mac, using free software package managers like Brew of MacPorts.

In the end the goal is to have a mac-centric toolkit for managing Node.js processes primarily for development purposes.

Installing Node.js on Mac OS X

For the first step towards this we just install Node on Mac.  There's a few ways of doing this, and I wrote up an excellent outline in Chapter 2 of my book Node Web Development - see link in the sidebar.

There is an installer available for Mac OS X at (nodejs.org) NodeJS.org, and you can certainly download that.  However, I prefer to use a package manager to install it because it's much easier to update as new Node releases are shipped.

MacPorts - assuming you already have MacPorts installed.

$ sudo port install nodejs npm

Homebrew - assuming you already have Homebrew installed

$ brew install node

Both install two commands, node and npm, in the path controlled by each tool.

You can test the installation by running a couple commands:

$ node --help
$ npm help

If these work, they'll print out appropriate outputs.

Next you can write a simple script like this (name it ls.js)

var fs = require('fs');
var files = fs.readdirSync('.');
for (var fn in files) { console.log(files[fn]); }

This implements an extremely simplified version of the /bin/ls command.

Next you might want to be doing some webapp development, and Express is the most popular framework for doing so.  My book goes into huge depths of showing how to develop Express applications.  Here's a quickie start:

$ sudo npm install -g express

What this does is use the npm (Node Package manager) application to install a global instance of Express.  The reason for this is to get the express command line tool into your path.  Normally we do not install packages globally (the -g flag) but some packages provide command line tools.

With Express installed this way you can now use it to generate a skeleton sample application:

$ express --ejs helloWorld

Then do this:

$ cd helloWorld
$ npm install

This pulls down all the dependencies of the sample application so that you can do this:

$ node app

This runs the application's server instance.  Then in a browser you can visit the application at (localhost) http://localhost:3000

About the Author(s)

(davidherron.com) David Herron : David Herron is a writer and software engineer focusing on the wise use of technology. He is especially interested in clean energy technologies like solar power, wind power, and electric cars. David worked for nearly 30 years in Silicon Valley on software ranging from electronic mail systems, to video streaming, to the Java programming language, and has published several books on Node.js programming and electric vehicles.

Books by David Herron

(Sponsored)