Skip to content
Programming contents

Lesson 2.4: JavaScript outside the browser: Node.js

Node.js runs JavaScript on your computer, without a browser. It's also what powers many servers.

The idea

Until now, JavaScript ran in the browser. Node.js runs it on your computer, from the terminal, from a file.

The language is the same. And it's what many servers use: you'll see it again in Back end.

Installing it

Download the LTS version from nodejs.org and install it. LTS means long-term support: it's the stable one.

Check it's there, in a new terminal:

Terminal
node --version
Terminal
v24.11.0

Your number may be different. What matters is that one shows up.

Running a file

In the VS Code terminal, already in my-game:

Terminal
node hello.js
Terminal
Hello from a file

node followed by the file's path. Since you're in its folder, the name is enough.

What Node doesn't have

prompt() and alert() belong to the browser: they open windows, and the terminal has no windows. In Node they give an error:

Terminal
ReferenceError: prompt is not defined

console.log() does work: it writes to the terminal.

Try it

Exercise

Run the table.js you created in 2.3 with Node.

Show the solution
Terminal
node table.js
Terminal
3 x 1 = 3
3 x 2 = 6
…
3 x 10 = 30

If you get Cannot find module, you're not in the file's folder: check with pwd and ls.

Summary

  • Node.js runs JavaScript outside the browser.
  • node file.js runs a file.
  • prompt() and alert() don't exist in Node; console.log() does.