In this lab exercise, you will install Node.js and practice executing JavaScript commands in the Terminal, where the Node.js REPL runs.
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.
There is no starter code for this lab exercise.
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:
brew install nvm
mkdir ~/.nvm
.zshrc
file and append the following lines:# NVM
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
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!)
command -v nvm
did not work, there’s a note that may help you in the Install section of nvm’s readme. It starts with the text “Note: On OS X, if you get nvm: command not found after running the install script, one of the following might be the reason:” – try those. Usually it has to do with where the nvm install script put the script that sources nvm for you every time you start your terminal.nvm install node
(see their readme for more information about how to download specific versions of node).nvm use node
in your terminal. This makes you use this version of node you just downloaded!
nvm use system
.which node
, you should see a long path including a new .nvm
hidden directory inside your home directory.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.
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.
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!
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.
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.
seaCreatures
. Give it 3 properties: fish
, mammals
and invertebrates
. Create an attribute for each property that is an array of sea creatures that belong to the type of group named in the property. For example, 'boats': ['sailboat', 'yacht', 'life raft']
.seaCreatures
.Talk with your pair partner about the differences between the REPL and a JavaScript file when creating an object literal and accessing its properties.