justjs: node.js tutorials

New here? You might want to start at the beginning.

5/15 '12

Getting Started, Part I: Installing Node

I've sung the praises of node.js, but I haven't yet explained how to actually bring this goodness into your life. Fair enough. Time to install node.

Famous last words maybe, but I'm going to devote just two short posts to the semi-interesting topic of installing things. It's all levitating outer space kitten games after that, I promise. So just bull through and you'll be glad you did.

I'm going to make a few assumptions:

1. You want to be able to try out node sites on your own computer. Yes, you could do everything in the cloud... and wait for Internet lag every time you want to test something. But would this completely defeat the purpose of using an environment as fast and fun to work with as node? Yes! It totally would!

2. You've got a Mac- or you're reasonably comfortable with the computer you do have and can use my step-by-step instructions as a rough guide to what you need to do. Both node.js and mongodb have installers for Windows, MacOS and Linux. (Actually, if you're on Ubuntu Linux, you can skip to the head of the class with a handy installer script I wrote at my awesome job. You're welcome.)

3. You don't have Homebrew or MacPorts or any other heavy duty geek oriented command line environment that already includes Node or offers it as a package. Or if you do, you're skipping this post. We'll use the official, easy installers. (Protip: if you do install Node and MongoDB via MacPorts, be prepared to wait a really long time.)

4. You have a decent text editor, one that can edit system files if necessary. If not, go get TextWrangler, a very solid free offering from the BBEdit people.

So much for exposition. After this it's just a chase scene.

Installing Node (on a Mac)

1. Go to nodejs.org. Click "Download," then "Macintosh Installer."

2. A pkg file will download. When the download is complete, open it and run the installer. The installer will install two things: node itself, and the npm package manager. npm lets you painlessly install optional modules for node. Which is good, because almost everything is an optional module.

3. Open a Terminal window. (Go to "Finder," then "Applications," then "Utilities," and launch "Terminal." Welcome to Unix! There's a nice terminal prompt tutorial here.)

4. Type:

node

And press enter.

If you get this:

-bash: node: command not found
Then you need to adjust your PATH.
 
"Hang on, I need to adjust my what?" When you run commands at the terminal prompt, your Mac needs to know where to find those commands. By default, your Mac looks in a short list of official places. Installers like the Node installer try to be nice and not muddy those places with their own contributions, so they pop things in folders like /usr/local/bin. To find things there, you adjust your PATH environment variable to include that folder.
 
Open TextWrangler or your editor of choice and browse to your personal home directory (the little house icon). Now check the "show hidden files" box. This allows you to see files like .profile, your personal profile for newly launched terminal windows. 
 
Also pull down the "Enable" menu and pick "Everything."
 
Now, if you see .profile, open it. If you don't see it, just create a new file; you'll save it as .profile.
 
Look for a line like this:
 
export PATH=/some/stuff:$PATH
 
If you don't see an "export PATH" line or you're making a brand-new .profile, you'll add your own:
 
export PATH=/usr/local/bin:$PATH
 
If you do see an existing line, just add /usr/local/bin: at the front:
 
export PATH=/usr/local/bin:/some/stuff:/some/other/stuff:$PATH
 
Now save your .profile (make sure you include that leading dot if you are creating it for the first time, and do NOT include any file extension like .txt).
 
Next, open a new Terminal window and type:
 
node
 
You should see:
 
>
 
If you still get a "command not found" error, check your work.
 
If you get the > prompt, great! Try this:
 
console.log("Hello World")
 
And press ENTER.
 
You should get back... oh all right, I think you can guess.
 
To exit, press control-D (isn't the terminal intuitive?) and then close your terminal window.
 

What's Next

 
You've got node on your computer! And so far we haven't done anything really cool. But that's coming very soon. In my next post we'll install MongoDB so we can actually store stuff. Then we're ready to move on to those levitating outer space kitten games (well... awesome web applications actually).
 

 

blog comments powered by Disqus