Master JavaScript

What JavaScript is, and how to run it

3 / 266
Contents

Contents

Master JavaScript

0 of 266 complete

12 min read

Getting it onto your own machine

A terminal, Node.js and a text editor, then one file that runs, and the four things that go wrong on the way there.

Every box in this course runs your code, so nothing in this lesson is needed to finish the course. It is still worth half an hour, because code that only runs inside somebody else's web page is not yet something you own.

Three pieces get you there, and each is installed once and then forgotten about.

  • A terminal, which is where you start a program by typing its name.
  • Node.js, the host that runs JavaScript outside a browser.
  • A text editor, which is where you write the file.

The terminal

A terminal is a window in which you run a program by typing its name and pressing return, rather than finding an icon and clicking it. Nearly every programming tool is built to be used this way, because a name you can type is also a name a script can type for you later.

  • macOS: hold Command and press Space, type Terminal, press return.
  • Windows: open the Start menu, type Terminal, press return. If there is no Terminal, Windows PowerShell is the same thing under an older name.
  • Linux: on most desktops, Ctrl and Alt and T together, or search the applications list for Terminal.

It opens showing a prompt: a short piece of text, usually ending in a symbol such as % or >, meaning the terminal is waiting for you. You type a command after it and press return.

In every terminal transcript in this course, a line that starts with a dollar sign is a line you type. The dollar sign stands in for whatever your own prompt looks like, and it is never part of what you type. Lines without one are the computer answering.

$ pwd
/Users/you
$ mkdir js-practice
$ cd js-practice
$ pwd
/Users/you/js-practice
$ ls
$
Four commands, and what came back from each.

A terminal is always somewhere, and that somewhere is a folder called its working directory. Any command you give that names a file without giving a full path means a file in that folder.

  • pwd prints the working directory, which is how you find out where you are.
  • mkdir js-practice makes a new folder with that name inside it.
  • cd js-practice moves the terminal into that folder. cd .. moves back out to the folder above.
  • ls lists what is in the folder. It printed nothing above because the new folder is empty.

Node.js

The first lesson called Node.js a host: the V8 engine wrapped in a program that supplies everything the specification leaves out, including files, networking and a console. Installing it is what puts a program called node on your machine for the terminal to find.

  1. Go to nodejs.org.
  2. Download the version marked LTS for your operating system.
  3. Run the installer and accept every default it offers.
  4. Close the terminal window you had open, and open a new one.

LTS stands for Long Term Support. Node.js publishes a new major version every six months, and every second one becomes an LTS release, which then receives fixes for about three years. The other line, labelled Current, gets new features sooner and stops being maintained sooner. Take LTS until you have a specific reason not to.

Step four is not superstition. A terminal reads its list of places to look for programs once, when it starts, so a window that was already open when you installed Node.js has never heard of it and never will.

$ node -v
v24.17.0
$ npm -v
11.13.0
Checking that it worked. Your numbers will be higher than these.

npm arrives in the same installer. It is the tool for fetching code other people have published, and it has a section of its own later in the course. For now its only job is to prove the installation is complete.

A text editor

You need something that saves plain text: the characters you typed and nothing else. A word processor is not that. It stores fonts, sizes and margins around the words, and an engine handed such a file sees a great deal that is not JavaScript. Visual Studio Code is the usual free choice; any editor that saves plain text will do.

One file, from nothing to output

Here is the whole ceremony, with every step written out. It takes about a minute once the three pieces are installed.

  1. In the terminal, cd js-practice so you are in the folder you made.
  2. In the editor, open a new empty file.
  3. Type the three lines shown below.
  4. Save it into js-practice under the name hello.js.
  5. Back in the terminal, type ls and check that hello.js is listed.
  6. Type node hello.js and press return.
console.log("Hello from my own machine.");
const price = 20;
console.log(price * 2);
hello.js, in full.
$ ls
hello.js
$ node hello.js
Hello from my own machine.
40
$
What the terminal shows.

Node.js read the file from top to bottom, carried out each line in turn, printed what it was asked to print, and finished. The prompt came back, which is how a terminal says the program has ended.

That file is the same three lines the box below runs. Nothing about them is specific to a page or to a terminal, which is the point of having done it once.

Try CodeJavaScript

Change the 20 to 35 here, then make the same change in your own hello.js and run node hello.js again. Both should agree.

The four things that go wrong first

What you seeWhat it meansWhat to do
command not found: node, or 'node' is not recognizedThe terminal has no program by that name.Open a brand new terminal window and try again. If it still says that, the installer did not finish.
Error: Cannot find module '/Users/you/hello.js'Node.js looked for that exact file and there is nothing there.pwd to see where you are, ls to see what is here, cd into the folder holding the file. Check the name for a hidden .txt.
Nothing at all, and the prompt returnsThe program ran and printed nothing.Wrap the value you expected to see in a console.log. The screen shows what you print and nothing else.
SyntaxError naming a line numberThe file could not be read as JavaScript, so none of it ran.The next lesson is entirely about reading this.
Every one of these is ordinary. None of them means the install is broken.

Stopping things

  • A program that is running and will not stop: hold Ctrl and press C. This works in every terminal on every system, and it is the first thing to reach for.
  • The REPL, once you are finished with it: type .exit, or hold Ctrl and press D.
  • The terminal itself: type exit, or close the window.
You save hello.js into your Documents folder, open a terminal, type node hello.js, and get Error: Cannot find module. There is nothing wrong with the file. What is wrong, and what do you type next?

The terminal's working directory is not Documents. A new terminal usually starts in your home folder, and node hello.js means "the file called hello.js in the folder I am in", which is a folder that has no such file. Type pwd to confirm where you are, then cd Documents, then ls to see hello.js listed, then node hello.js again. Giving the full path, node ~/Documents/hello.js, works from anywhere and is the other answer.

Check yourself

You install Node.js while a terminal window is already open. In that window, node -v still says command not found. Why?

All lessons in Master JavaScript