Command Line and Node

Chris Haralampoudis
3 min readDec 29, 2020

This lesson we will be introducing the command line. The command line is how we can navigate the files in our computer and run the programs that we will code. We will cover navigation, directory creation, file creation, writing to a file, and running a script.

Apps to Use

Mac Users:

  • Use Terminal (already installed)

Other OS:

Bread and Butter Commands

cd <directory-name> → stands for "change directory"

  • Supports tab completion. Start typing directory name and then hit tab when it is the only one that meets that pattern

ls → stands for "list"

mkdir <directory-name> → stands for "make directory"

Directory names are case sensitive! Most code you write will be.

Example:

ls

Output: Desktop Documents Movies Music

cd Music

ls

Output: Kanye

mkdir Odezsa

ls

Output: Kanye Odesza

For this lesson we will be initializing our project directory. Navigate to the location on your computer where you would like to make your folder. Name it ModernWebDev2021.

Node

Node is the JavaScript (JS) server runtime. So what does that mean:

  • It is powered by JavaScript.
  • It is what will run your application from your computer. Server code is code that runs on your actual computer. Server is synonymous with computer. This means that node is essentially built differently than the JS runtime your browser runs.
  • It can interact with your computer’s files system among other functionality.
  • Feel free to read more about his here.

TLDR: Node is what we will use to run our web application from our computers.

Installing Node

Go to this site and download the installer (not the binary) for your operating system. If you do not know your operating system, try Googling. If you still do not know, please post a question in the slack channel.

  1. Execute the installer and follow the steps.
  2. Restart your command line application.
  3. Navigate to your class folder ModernWebDev2021.
  4. Run node -v which should output your current node version.

Combining it All:

Now we will make a script using the command line and run it using Node via the command line. A script is just series of commands that JavaScript can execute. We run a script by using the node command and passing it the name of the file. For our example, we will create a file named lesson1.js and call it by executing node lesson1.js.

  1. Navigate to your class folder.
  2. Create your file touch lesson1.js
  3. ls → Verify that your file has been created.
  4. Run node lesson1.js → Nothing will happen, the file is currently empty.
  5. Run echo 'console.log("hello world")' > lesson1.js → This will paste the string into the specified file.
  6. Run node lesson1.js → Should output hello world on the line below.

You have just run your first node script. Good stuff.

Feel free to play around with Node in the command line or add some more JavaScript to your lesson1.js file (I would recommend using a code editor which we will install next lesson). To run node commands in the command line, just execute node and you can write JavaScript. Try 2 + 2. To exit Node mode, do cntrl + c or cmnd + c on a Mac.

This is one of the more boring lessons in my opinion, but super valuable. We will get into the fun stuff soon.

--

--