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:
node --version
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:
node hello.js
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:
ReferenceError: prompt is not defined
console.log() does work: it writes to the terminal.
Try it
Run the table.js you created in 2.3 with Node.
Show the solution
node table.js
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.jsruns a file.prompt()andalert()don't exist in Node;console.log()does.