Getting Started With Node-JS

I attended the Node-JS Camp this past week and had a lot of fun talking with the developers, and familiarizing myself with the awesomeness of Node-JS. Node-JS is a powerful version of JavaScript that allows for server-side scripting and running light-weight web servers. The best part is, if you know client-side JavaScript, you know the basics of server-side JavaScript, and only need to learn how to import and use the built in libraries. Todays tutorial will walk you through the steps to get started using Node-JS.

Preparing to install node

You will need to have a unix based terminal client and GCC installed. Windows users will need to follow the Cygwin installation instructions. Mac users should install Xcode.

Once that is done, make sure that the GCC compiler is properly installed by running:

which gcc

If it is working, you should expect something like /usr/bin/gcc to be printed out.

Installing Node

There are a lot of methods for install Node; you can see them all in this gist. I prefer the second method and will use it for this tutorial:

Create a location for Node on your machine (I recommend the (~/) home or /usr/local/ directory) and add it to your PATH:

mkdir -p ~/local
echo export PATH=$HOME/local/bin:$PATH >> ~/.bashrc

Fetch node using GIT:

cd ~/local
git clone git://

Configure and install node:

cd node
./configure --prefix=~/local
make install

Test that the installation worked by typing node. This should open up a program prompt, where you can start writing JavaScript code. Try the following test code:

var i = 1234;

Type ctrl+d to exit the prompt. You can now write server-side JavaScript code and run it using node. For example, if you saved the above script as test.js, you could run it by:

node test.js

Installing NPM

NPM is the package manager for node. It is written and maintained by Isaac Schlueter. Each package is maintained by an individual contributer and anyone can contribute packages. NPM is the tool you use to install packages like YUI, Connect (HTTP Middleware), Express (Web Framework), Socket.IO (Web Sockets), mysql, and a whole lot more.

Install NPM by:

cd ~/local
git clone git://
cd npm
make install

Now you can install packages using:

npm install <package-name>
npm install yui3-core

To see all the available packages type:

npm ls

To get NPM help type:

npm help

To get package help type:

npm doc <package-name>
npm doc yui3-core

Writing a Node script

Node scripts are the JavaScript we know and love, plus a couple of new features. For example you have the require function, which fetches and initializes a package for you. The require function returns an instance of the package you are importing. You can require all the packages found at Node-JS API and any packages installed using NPM. Here is an example of how to include and use the YUI3-Core package:

YUI = require(yui3-core).YUI;
YUI().use(function(Y) {
	s = "Hello World";

The console object has been used in several examples, and is built into Node, providing a simple way for printing variables.

For more information see

Some of the code used in the examples above was originally found at:

Installing Node and NPM by Isaac Schlueter.