Getting Started with NodeJS

A basic guide to take you from knowing nothing but a little JavaScript to experimenting with NodeJS.

Installation

For Windows and OSX, you can simply head on over to nodejs.org to download an install package.

If you prefer to use a package manager to handle installation, see this page from the node wiki which provides instructions for a fairly comprehensive list including chocolatey, homebrew, and apt-get.

Next, test out your install by running these commands:

~$ node --version
v0.10.26

~$ npm --version
1.4.3

In Windows, use the Node.js command prompt shortcut that is created in the Start Menu to launch the console with proper environment variables and paths initialized. The commands are the same:

C:\>node --version
v0.10.28

C:\>npm --version
1.4.9

Maybe you're wondering, "Wait, what's this npm thing?" That's the NodeJS Package Manager. It's an integral part of node, so you should make sure that it can run before you move on.

Hello NodeJS!

Getting output to the console is as good a place to start as any. The NodeJS version of "Hello World" is ridiculously simple, but hey, baby steps, right?

First, create a folder to play around in:

~$ mkdir hello
~$ cd hello

Open your editor of choice and drop in this one line:

console.log('Hello NodeJS!');

Save the file in the hello directory. It doesn't particularly matter what file name you choose, but app.js is commonly used for the main entry point of a node application.

Finally, test out your hot new node app with this one simple command:

~/hello $ node app.js
Hello NodeJS!

And you're off!

NPM

The node package manager is for installing and updating node modules that you can use in your own applications or modules.

Here's a simple example. There is a colors module that provides formatting capabilities for the console.log method. To try it out, first install the module in your application's directory:

~/hello $ npm install colors

Note: if you want to make a module accessible system-wide, just add the -g option to the install command:

~/hello $ npm install -g colors

Now the module can be referenced from your killer console application. Open up app.js if it's not already and modify the contents to match this:

require('colors');
console.log('Hello NodeJS!'.green.underline);

Save the file again. Running node app.js now should be a little more... green and... underliney.

Maybe one day the colors npm module gets updated and you want to try out the new colory goodness. Easy enough:

~/hello $ npm update colors

There are lots of npm modules available. Check out some popular ones or search the repository by visiting npmjs.org. Each module has a home page with some documentation covering usage.

Structuring your NodeJS application

You're not one of those programmers that puts all of your code in one file, are you? Cool. Another straight-forward task in node is splitting out application logic into your own modules.

I'll get right to the example. Create another file named log.js inside the hello directory and paste in these contents:

require('colors');

var info = function (message) {
    console.log(message.green);
};
var warn = function (message) {
    console.log(message.orange);
};
var error = function (message) {
    console.log(message.red);
};

module.exports = {
    info: info,
    warn: warn,
    error: error
};

What's going on here? By setting the value of the module.exports variable, we've explicitly provided the object that should be returned when this module is included by a node program. In this case, the object will have 3 methods that reference the implementation inside log.js.

Let's update hello.js to use our new console logger:

var log = require('./log.js');

log.info('Something happened.')
log.warn('Something of some concern occurred.');
log.error('Things are catching on fire.');

The main thing to note here is that the path to log.js is explicitly referenced. If log.js were in a subfolder named lib, the require statement should read:

var log = require('./lib/log.js');

Now when you run node app.js you should see some colorful output that makes use of your first node module.

Next Steps

This post covered the basics of getting NodeJS up and running, working with NPM, and structuring your application. I encourage you to check out some more advanced node topics on this blog, search through the npm repository, and just start building something.

Author

Jonathan Keith

Software developer living in Tokyo, raising children, building applications, and learning Japanese.

comments powered by Disqus