Getting Started with Node.js: Part 1

Our "Getting Started" series consists of quick tutorials on the technologies most widely used on the Web today.  This is our first of the series, and is a quick guide for the beginner on how to get Node.js up and running on your machine (only Mac for now).  Node.js is a platform enabling Javascript on the server, and it's one of the cornerstones of our Immersive Web Development curriculum here at DigitalCrafts.  Whether you're learning to code with us in Atlanta, or you're an experienced developer seeing what the Node fuss is all about, welcome and enjoy the read! (To skip ahead, here is part 2 and part 3.)

  • First, go to https://nodejs.org/download. As of this publication, you should see a screen that looks something like this with your options:

  • You need to select the right version and you're on your own here. The good people at nodejs.org have created installers which I suggest choosing and running. If you want to use one of the binaries, you probably don't need this tutorial.
  • Run the installer and follow the trail.

  • Congrats. You have now installed node on your Mac.
  • To get to the node.js command shell, open up a terminal window. If you haven't done this before, type command+space and enter "terminal"
  • Depending on your version of MacOS, click/double click on your terminal icon to open it. You will see a window like this: 

  • Node is installed locally so you can type “node” from any directory to run the node.js command shell. Nice work!

  • Let's make a "Hello world" application. 
  • Open a text editor. If you have Sublime text, Coda, etc. you can use that. If not, open up TextEdit which ships with every Mac. You can just type command+space again and search for "textedit" to find it. Again, depending on your version of MacOS, click/double click to open.
  • We want this to be a plain text document so if you have icons at the top of your screen, type command+shift+T to go to plain text. 
  • BEFORE:

  • AFTER:

  • Enter this text without anything else: 

console.log('Hello World');

  • Save the file as hello.js to your desktop.
  • Now go back to your terminal window (it's an ugly black square in your toolbar). Type in: 

cd ~/Desktop

  • This will get us to your desktop within the terminal. Now type:

node hello.js

to run it.

Woah!! You just said Hello World through node.js. 

Note, if you are using Textedit to make this file, it is critical that you switch to plain text before making the file. Text Edit, like most Document editors, will turn your quotes/ticks into these things (squiggly): ‘. Your program will not function.  Most developers use text editors with more features and funcationality, which is why I strongly advise checking out Sublime Text 3 or TextWrangler.  Both are free (with paid versions if you want the "Upgrade now!" window to stop popping up, but that's no big deal).

Stay tuned for more in the "Getting Started" series!

 

Author
Rob Bunch

Immersive Web Instructor, Atlanta