Installing & Running Node
Install Node, check it works, and run your first JavaScript file from the terminal.
What you will learn
- Install Node.js and verify it
- Run a .js file with node
- Use the Node REPL
Install and verify
Download Node from nodejs.org (choose the LTS version — LTS stands for Long-Term Support, which just means the stable, well-tested release that is safest for beginners). It includes npm (the package manager — a tool that downloads ready-made code for you). Verify in your terminal:
node -v # prints the Node version, e.g. v22.x
npm -v # prints the npm versionNote: Output:
v22.3.0
10.8.1
(Your numbers may differ.) node -v asks Node to print its own version, and npm -v does the same for npm. If both print a version number, your install worked. If you instead see "command not found", Node is not installed correctly or your terminal needs restarting.
Run a file
Create a file hello.js, then run it with the node command. Note: console.log prints to the terminal here, not a browser.
// hello.js
console.log("Hello from Node.js!");
const sum = 5 + 3;
console.log("5 + 3 =", sum);This is just ordinary JavaScript — the same console.log you used in the browser. The difference is where it runs: there is no web page here, so the messages appear in your terminal instead of the browser console. The second console.log prints a label and then the value of sum (the two are separated by a comma so Node puts a space between them).
node hello.js
# Output in the terminal:
# Hello from Node.js!
# 5 + 3 = 8Note: Output:
Hello from Node.js!
5 + 3 = 8
node hello.js tells the Node runtime to read the file hello.js and run every line in it from top to bottom. The two console.log lines print, in order, exactly what you see. (The lines starting with # in the box above are just comments reminding you what to expect — you do not type them.)
Tip: Type node alone to enter the REPL — an interactive prompt where you can run JS line by line. Type .exit to leave. Great for quick experiments.
Q. How do you run a file called app.js with Node?
✍️ Practice
- Install Node and confirm
node -vandnpm -vboth work. - Create and run a
hello.jsthat prints a few messages.
🏠 Homework
- Write a Node script that calculates and prints the area of a rectangle.