Node.js Setup

Chapter 2 7 mins

Learning outcomes:

  1. Installing up Node.js on Windows
  2. The global node command
  3. The Node.js REPL

Introduction

In the previous chapter, we got a pretty warm introduction to perhaps one of the most revolutionary technologies of the era — Node.js. We learnt what exactly Node.js is — a runtime environment for executing JavaScript — and a bunch of its cool features.

So now that we have a rock-solid understanding of precisely what we're dealing with, it's time to see how to set up Node.js on our operating system.

In this chapter, we demonstrate installing Node.js on Windows but the given ideas are, more or less, the same on macOS and Linux. Besides this, we'll also briefly explore the node command and the Node.js REPL environment.

Let's get installing...

Installing Node.js on Windows

When Node.js was first released to the public in 2009, it didn't have support for Windows. Almost a year later this became a reality, and today it's a norm.

Following the popularity of the Windows OS, we'll demonstrate setting up Node.js on Windows in this section.

  1. Download Node.js

    Head over to https://nodejs.org/en/download and click on the big link reading 'Windows Installer', as highlighted below:

    Downloading Node.js from the official website
    Downloading Node.js from the official website

    This will start the download of Node's installer for Windows, which is typically a small file worth a handful of MBs.

  2. Run the installer

    Once the download is complete, run the installer.

    This opens up the following installation dialog:

    Node.js installer on Windows
    Node.js installer on Windows

    Proceed with the recommended settings and get Node.js installed in the matter of one or two minutes, if not a few seconds.

    And that's it! Yes, that simple.

The global node command

At this stage, we are completely done with our installation of Node. Everything is right there on our computer, now it's just a matter of using it for executing JavaScript on our machine in the Node runtime.

To confirm that Node.js exists on the system, just type in node -v in the terminal:

node -v
v20.11.1

If Node.js is installed on the system, you'll get back a version string specifying the version of the Node.js runtime installed, as shown above.

The current working directory — where we are currently in the terminal — doesn't matter for the node command because it is a global command, thanks to its addition to PATH in Windows during installation.

However, if you notice an error message in return of node -v, it means that your Node.js setup isn't right and you should potentially re-consider doing it all over again (which is usually the best bet), as instructed above.

Your version might be different than ours following a version update of the runtime by the Node community. You should mainly only be concerned with some version string returned by the node -v command, not necessarily which one.

This node command is the cornerstone of the Node.js runtime environment — it's what allows us to execute a JavaScript program using the runtime. In the next chapter, we shall use it to execute a basic JavaScript program.

The REPL environment

Before we end this chapter, it's worthwhile discussing an important concept tied to the node command.

When we go on and enter node in the terminal, without anything following it, we enter into what's called a REPL environment.

Here's an illustration:

C:\Users\codeguage>node Welcome to Node.js v20.11.1. Type ".help" for more information. >

You'd already be familiar with a REPL from the browser's console where you have the freedom to execute any JavaScript statement and see its result live.

REPL stands for Read-Evaluate-Print Loop and represents a computer program meant to continuously ask input from the user and then printing the result of whatever the user enters in response.

As with the browser REPL (i.e. the console), the Node REPL is a great place to experiment around with the interface provided in the Node runtime.

C:\Users\codeguage>node Welcome to Node.js v20.11.1. Type ".help" for more information. > 2 + 2
4
> 'Hello' + ' world!' + ' From Node REPL'
'Hello world! From Node REPL'
>

In this course, we'll be using the Node REPL quite a lot so it's good to know a little bit about it.

Once we enter the REPL, every input prompt comes from the REPL program.

If we want to exit out of the REPL and get something else done in the terminal window, we can simply enter the special command .exit:

C:\Users\codeguage>node Welcome to Node.js v20.11.1. Type ".help" for more information. > .exit C:\Users\codeguage>

The .exit command instructs the REPL parser to exit the REPL and tear down the Node runtime before returning control back to the terminal.

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage