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.
-
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 websiteThis will start the download of Node's installer for Windows, which is typically a small file worth a handful of MBs.
-
Run the installer
Once the download is complete, run the installer.
This opens up the following installation dialog:
Node.js installer on WindowsProceed 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:
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.
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:
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.
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.
4
'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
:
The .exit
command instructs the REPL parser to exit the REPL and tear down the Node runtime before returning control back to the terminal.