Getting started with Node.js, Part 1

Languages

Frameworks

Topics

  1. Installing Node.js
  2. Using the Node.js REPL

Overview

In this lab exercise, you will install Node.js and practice executing JavaScript commands in the Terminal, where the Node.js REPL runs.

Context

Setup Instructions

Use the Terminal for navigating around the file system and creating new folders and files. Refer to the lesson on Shell Commands if you need guidance on using the Terminal.

When you get to the steps below that ask you to initialize a git repo and track files using git, refer to the lesson on Git and Version Control if you need guidance.

Navigate to the techtonica-labs folder located in your Desktop. Create a new folder called getting-started-with-node within the techtonica-labs folder. Navigate to getting-started-with-node. Initialize getting-started-with-node as a git repository.

If you have questions, do not disturb other pairs until you have spent 15-20 minutes troubleshooting within your own pair. Post a message on the #help channel in Slack if you and your pair partner are still stuck after 15-20 minutes. Be sure to format your question using the template we practiced in the Asking Good Questions lesson.

Lab Instructions

Starter Code

There is no starter code for this lab exercise.

Lab Exercise


Part 1 - Install Node.js using nvm

nvm, or Node Version Manager, allows you to have multiple versions of node on your machine at once. It also installs node into your home directory, where you definitely have read and write permissions (this gets rid of problems when trying to use npm install -g for packages later.

Visit the nvm repository on GitHub to get started.

Follow the installation steps in nvm’s readme. Most importantly, these steps:

  1. Install nvm via Homebrew: brew install nvm
  2. Create a system directory for nvm: mkdir ~/.nvm
  3. Open your .zshrc file and append the following lines:
# NVM
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
  1. Verify your nvm installation by typing command -v nvm in your terminal. You should just see the output nvm in your terminal. (See their readme to read about why which nvm does not work. Do not be alarmed that which nvm returns nothing!)
  2. Download the latest version of node using the command nvm install node (see their readme for more information about how to download specific versions of node).
  3. Type nvm use node in your terminal. This makes you use this version of node you just downloaded!
  4. When you type which node, you should see a long path including a new .nvm hidden directory inside your home directory.

Part 2 - Run JavaScript commands in the Node.js REPL

4. Open the Node.js REPL
Now that you have downloaded Node.js, we can explore Node’s REPL (Read-Evaluate-Print Loop). Open the REPL by running the command node in your Terminal. You should see a new carat ( > ) appear on the next line, which indicates that you are no longer communicating with your Terminal. Instead, the REPL is awaiting your next command.

Open the Node REPL
Open the Node REPL

5. Close the Node.js REPL
If you ever get stuck in the REPL, you can exit it and get back to the Terminal by running the <Control> + C command twice. Try this now. You should see in your Terminal something similar to what is shown below. (The Terminal uses ^C to mean <Control> + C.) Each person in your pair should practice opening and closing the Node REPL 2 times.

Close the Node REPL
Close the Node REPL

6. Execute JavaScript code in the Node.js REPL
Once each person in your pair has practiced opening and closing the Node REPL, open the REPL again by running the command node in your Terminal. This time, each person should play around with running different JavaScript commands, like these:

Note that you can leave off the semicolons in REPL.

Congratulations! You just ran server-side JavaScript for the first time!

Execute JS in the Node REPL
Execute JS in the Node REPL

7. Execute JavaScript code in Chrome’s JavaScript console Open the Chrome JavaScript console by pressing the <OPTION> <COMMAND> J keys. Run all the JavaScript code from Step 6 in this console. This is something you should already be familiar with, as all JavaScript up to this point has been run in the browser.

Running JS in the Browser
Running JS in the Browser

Questions to Consider

Extensions

Talk with your pair partner about the differences between writing a for-loop in a JavaScript file and writing a for-loop in the Node REPL.

Talk with your pair partner about the differences between the REPL and a JavaScript file when creating an object literal and accessing its properties.