Using NodeJS

Running JavaScript Code

JavaScript is the language of the Internet! Whenever you browse your favorite website (google, facebook, twitter, appacademy.io), your web browser is executing JavaScript code.

There are two main environments you will use to run JavaScript: the browser (Chrome, Firefox, etc.) and Node. Writing code for the browser (a.k.a. frontend engineering) requires understanding a lot more than just JavaScript, so we'll come back to that topic later in the course. For now, you will concentrate on running JavaScript on your computer using Node.

So what is Node exactly? Node.js is a very powerful runtime environment built on Google Chrome's JavaScript V8 Engine. It is used to develop I/O--i.e., Input/Output--intensive applications like video streaming sites, robots, and other general purpose applications. For your purposes, the most advantageous feature of Node is that it provides a way for you to run JavaScript outside of the browser.

Now that you have Node installed on your local computer, it's time to run some JavaScript! Running your own code on your own computer is a rite of passage for all developers. We know you are up to the challenge!

By the end of this reading you should be able to:

Node REPL vs. JavaScript File

You can run JavaScript code with Node in two ways:

  1. Using the Node REPL
  2. Running a .js file

Using the Node REPL and using a JavaScript file are both common ways to execute JavaScript code, but they are useful in different scenarios:

The Node REPL (Read, Evaluate, Print, Loop) enables you to test ideas quickly. It is useful when playing around or exploring possibilities because you can quickly see how Node evaluates an expression. Any code that you type into the Node REPL will be lost, however, when you exit the REPL. If you've ever used a program that let you write a line of code and execute it immediately without a separate command, then you've used a REPL.

JS files, in contrast, let you save code for the long term. If you create a .js file and save it, then all the code within the file can be referenced and used later. When you work on problem sets, projects, and anything else you want to save, you should always save your code to a .js file!

Using the Node REPL

To use the Node REPL, simply open up your command line (Terminal) and enter the command node. In the examples below, the $ prompt shows that you are on the command line in Terminal. (You may see a different command line prompt on your computer, e.g., %.)

~ $ node
Welcome to Node.js
Type ".help" for more information.
>
    

Notice that as soon as you enter the node command, you get a welcome message and see your Terminal prompt change to >. The > prompt means that you are inside the Node REPL, so you can type any valid JavaScript lines and see what they evaluate to:

~ $ node
Welcome to Node.js
Type ".help" for more information.
> 1 + 1
2
> let message = "Hello" + "world"
undefined
> message
'Helloworld'
    

You can also define functions in the Node REPL, although you will find writing them in that environment is not super fun because the Node REPL is not optimized for that kind of coding. (You'll learn more about functions soon. For now, just know that a function groups code statements together. The grouped code will then run whenever the function is invoked, i.e., called.)

Here is an example of defining and invoking a function using node:

~ $ node
Welcome to Node.js
Type ".help" for more information.

> function sayHello () {
... console.log("hello!");
... }
undefined
> sayHello();
hello!
    

If you want to exit the Node REPL, and head back to your plain old command line, enter the command .exit in the REPL. Doing this will get rid of the > icon, which means you are no longer in the REPL. When you are back at the command line, you can enter the normal commands (e.g., cd, ls, pwd):

$ node
> 1 + 1
2
> "How do I get out of here" + "!?!?"
'How do I get out of here!?!?'
> .exit
~ $
    

Using JavaScript Files

The first thing you'll need in order to run a JavaScript file is to create a file that will contain the code you will be running. A new file is like a blank canvas just awaiting the chance to be made into art.

If you don't currently have a dedicated coding folder, start off by creating a new folder somewhere accessible, like your Desktop folder. Then you can open that folder using VS Code. From there you can simply create a "New File". In order to create a JavaScript file, make sure that the file name ends in .js, for example, myFile.js.

Now take a moment to enter some code into your new .js file like the following:

// AppAcademyWork/myFile.js
console.log("hello world!");
    

Don't forget to save the file with your new code!

Now to run a JS file you first need to go into the folder that contains that file by using cd in your command line. Feel free to use ls to list your folders and see where you have to go. Once you are inside of the correct folder, run node <fileName>, for example node myFile.js. When you enter these commands, be aware of the capitalization. File names are case sensitive!

~ $ ls
Downloads
Desktop
Music
Videos

~ $ cd Desktop
~ Desktop $ ls
AppAcademyWork

~ Desktop $ cd AppAcademyWork

~ AppAcademyWork $ ls
myFile.js

~ AppAcademyWork $ node myFile.js
Hello world
    

That is how you run JavaScript on your local computer! You create and save a file, navigate to that file in your terminal, then run the file using the node command followed by the filename (i.e., node <fileName>).

What you've learned