Lesson Weekend

Important Note: The process depicted in the video is meant for Windows and Linux operating systems. On a Mac, install Node on the command line using Homebrew, as described in the text section of this lesson.

Important Note: The process depicted in the video is meant for Windows and Linux operating systems. On a Mac, install Node on the command line using Homebrew, as described in the text section of this lesson.

OSX Installation Instructions


On OS X systems, install Node.js through Homebrew with the following command in your home directory:

$ brew install node

Confirm that node and npm (node package manager, installed automatically with Node) are in place by checking the versions (Node should be 4.0.x or higher, npm should be 3.6.x or higher):

$ node -v
v6.2.0
$ npm -v
3.10.5

(If you have an older version of Node already installed, upgrade through Homebrew by running $ brew upgrade node.)

Complete the "Working with Node" section below to confirm your installation is functioning correctly.

Homebrew Installation

If you do not have Homebrew installed yet, you may install it now by copy and pasting this command:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

This installs Homebrew on your device.

Next, ensure Homebrew packages are run before the system versions of the same (which may be dated or not what we want) by executing the following:

$ echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile

Windows / Linux Installation Instructions


To install Node on other systems, go to the Node website, then download and install the appropriate installer for your operating system, as seen in the video associated with this lesson.

Then, complete the "Working with Node" section below to confirm your installation is functioning correctly.

Working with Node


Now, let's see how Node works. Let's create a file called hello.js, in a new project directory called hello:

hello.js
console.log("hello world");

Before we start playing around with Node, let's revisit how this works in the browser's runtime environment. We'll whip up a simple HTML document that runs this script:

hello.html
<html>
  <head>
    <script src="hello.js"></script>
  </head>
  <body>
    <h1>Open your console to see that our JavaScript program has run!</h1>
  </body>
</html>

With both files in the same folder, we can double-click the html document to open it in a browser. Sure enough, there's "hello world" logged in the console.

Now, let's run this file with Node:

$ node hello.js

This time, "hello world" is printed to the terminal.

So, you can see that both the web browser and Node.js are JavaScript runtime environments. Both run the same JavaScript code. In fact, Node.js's underlying JavaScript engine is V8, which is the same engine used in Google Chrome. The major difference between Node and a browser like Chrome is that the browser provides the runtime environment in the browser, whereas Node provides it on the command line. And they have slightly different capabilities, with the browser providing tools like the DOM, and Node providing tools like file system access.

Sample code

hello.js
console.log("hello world");
hello.html
<html>
  <head>
    <script src="hello.js"></script>
  </head>
  <body>
    <h1>Open your console to see that our JavaScript program has run!</h1>
  </body>
</html>