Warm-up · Activity 1 of 7
// B1.2 · ~32 min · Beginner
The node command, the REPL and --watch
After this lesson you can run JavaScript with node as a file, a string or a REPL session, find out which Node.js version runs it, and restart your program on every save.
You will be able to
- Run code with node main.js, node -e and node -p, and read the version with node --version or process.version
- Use the REPL: see values without console.log, reuse _, type multi-line code and leave it again
- Restart a program on every save with --watch, and predict what --watch-path watches
Predict · Activity 2 of 7
Predict before you read on. You start node and type these two lines, pressing Enter after each. What does the REPL print after the second line?
6 * 7 _ + 1Practice · Activity 3 of 7
Fill in the flag so that the command prints 1024, the value of the expression, without any console.log.
node ____ "2 ** 10"node "2 ** 10"Practice · Activity 4 of 7
Match each command to what it does.
Practice · Activity 5 of 7
In the REPL you type the first line and press Enter. The REPL waits for more, so you type the other two. What does it print?
[1, 2, 3].forEach((n) => { console.log(n * 10) })Brain teaser · Activity 6 of 7
Brain teaser. main.js imports ./greet.js, which is not in src. You start the command below, save a change to greet.js, then save a new file src/notes.txt. How many times does main.js restart?
node --watch-path=src main.jsApply · Activity 7 of 7
Mini-task. In a new folder, create package.json with "type": "module" and a "dev" script that runs node --watch main.js, and a main.js that prints one line. Start it with node --run dev, change the line in main.js and save. Check that the terminal says Restarting and shows the new line. Then run node --version in a second terminal and write down the version you use.
Check your work against this list
Build it yourself
Read the worked example, then write the exercises. Your code runs in your browser or on your computer and is never uploaded.
Worked example
Which Node.js is running?
Before you debug anything, check which Node.js runs your code. This program asks the running process. process.version is the same string that node --version prints, process.versions lists the versions of Node.js and the parts built into it, such as the V8 JavaScript engine, and process.release.lts names the long-term-support line. The output below comes from Node.js 24.21.0; your computer shows its own version.
main.js
// Which Node.js is running this file? The process object knows.
console.log("version:", process.version);
console.log("without the v:", process.versions.node);
console.log("V8 engine:", process.versions.v8);
console.log("LTS name:", process.release.lts);
Run it with
node main.jsOutput
version: v24.21.0
without the v: 24.21.0
V8 engine: 13.6.233.17-node.53
LTS name: Krypton- process.version starts with a v; process.versions.node is the same version without it.
- process.versions.v8 is the version of V8, the JavaScript engine that runs your code.
- Krypton is the name of the Node.js 24 LTS line. On a Current release, which is not LTS, process.release.lts is undefined.
- node --version in the same terminal prints the same v24.21.0: it is the same node.
Exercises
Exercise 1 of 2
From the REPL to a file
These three lines come from a REPL session, where they printed 42, then 50, then 'NODE'. Saved as main.js, they stop with ReferenceError: _ is not defined, and a file prints nothing it is not told to print anyway. Rewrite main.js so that node main.js prints 42, 50 and NODE, one per line. Keep the calculations: store 6 * 7 in a variable instead of relying on _.
Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads the JavaScript runner (up to 0.1 MB) and keeps it cached. Your code runs in your browser’s own engine and stays on your device.
Hints
Hint 1
In a file, a value is printed only when you pass it to console.log.
Hint 2
_ exists only in the REPL. Give the first result a name: const answer = 6 * 7;
Hint 3
console.log("node".toUpperCase()) prints NODE without quotes.
Show a solution
One way to solve it. Yours can look different and still pass the checks.
const answer = 6 * 7;
console.log(answer);
console.log(answer + 8);
console.log("node".toUpperCase());
Run it on your computer
Install Node.js 24 LTS or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.
main.js
// Copied from a REPL session. It printed 42, then 50, then 'NODE'.
6 * 7;
_ + 8;
"node".toUpperCase();
main.test.js
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {runMain} from './learnrun.js';
test('runs without an error', async () => {
await runMain();
});
test('prints 42, 50 and NODE, one per line', async () => {
const got = (await runMain()).trimEnd().split('\n');
assert.deepEqual(got, ['42', '50', 'NODE'], `the program printed ${JSON.stringify(got)}`);
});
package.json
{
"type": "module"
}
package.json tells Node.js that the .js files are modules; keep it in the folder.
Run the program:
node main.jsRun the checks (needs learnrun.js in the same folder):
node --testDownload learnrun.jsExercise 2 of 2
The major version
Write major(version), which takes a version string like "v24.21.0", as process.version gives it, and returns the major version as a number: 24. It must work for any number of digits, such as "v8.17.0" and "v100.0.0". The last line of main.js already prints the running version with it, for example Node.js v24.21.0 major 24. Run it with node main.js and check the tests with node --test.
This exercise needs Node.js on your computer (the browser version cannot run it). The files and commands are below.
Hints
Hint 1
Remove the leading v first: "v24.21.0".slice(1) is "24.21.0".
Hint 2
split(".") cuts the rest into ["24", "21", "0"]; the first item is the major version.
Hint 3
The pieces are strings. Number("24") turns one into the number 24.
Show a solution
One way to solve it. Yours can look different and still pass the checks.
// Returns the major version as a number: "v24.21.0" -> 24.
export function major(version) {
return Number(version.slice(1).split(".")[0]);
}
console.log("Node.js", process.version, "major", major(process.version));
Run it on your computer
Install Node.js 24 LTS or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.
main.js
// Returns the major version as a number: "v24.21.0" -> 24.
export function major(version) {
return 0;
}
console.log("Node.js", process.version, "major", major(process.version));
main.test.js
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {runMain} from './learnrun.js';
import {major} from './main.js';
test('major("v24.21.0") is 24', () => {
assert.equal(major('v24.21.0'), 24, `major("v24.21.0") returned ${JSON.stringify(major('v24.21.0'))}`);
});
test('works for one-digit and three-digit majors', () => {
assert.equal(major('v8.17.0'), 8, `major("v8.17.0") returned ${JSON.stringify(major('v8.17.0'))}`);
assert.equal(major('v100.0.0'), 100, `major("v100.0.0") returned ${JSON.stringify(major('v100.0.0'))}`);
});
test('prints the version of the Node.js that runs it', async () => {
const expected = `Node.js ${process.version} major ${process.versions.node.split('.')[0]}`;
const got = (await runMain()).trimEnd();
assert.equal(got, expected, `the program printed ${JSON.stringify(got)}, expected ${JSON.stringify(expected)}`);
});
package.json
{
"type": "module"
}
package.json tells Node.js that the .js files are modules; keep it in the folder.
Run the program:
node main.jsRun the checks (needs learnrun.js in the same folder):
node --testDownload learnrun.jsCommon mistakes
Pasting a REPL session into a file
> 6 * 7
42
What Node.js prints
SyntaxError: Unexpected token '>'Why, and the fix
The > at the start of a line is the prompt of the REPL, and the line under it is its answer: neither is JavaScript. Copy only the code you typed, and pass each value you want to see to console.log: console.log(6 * 7). Because this is a SyntaxError, no line of the file runs at all.
Using _ in a file
const total = 6 * 7;
console.log(_ + 1);
What Node.js prints
ReferenceError: _ is not definedWhy, and the fix
_ is a convenience of the REPL, which stores the last result in it. A file has no such variable, so _ is an unknown name there. Name your values instead: console.log(total + 1).
Using fs without importing it
console.log(fs.existsSync("main.js"));
What Node.js prints
ReferenceError: fs is not definedWhy, and the fix
The REPL loads built-in modules such as fs the first time you type their name, so this line works there. A file gets no such help: import what it uses. Put import {existsSync} from "node:fs"; at the top and call existsSync("main.js").
JavaScript in the browser: your browser’s own engine, in a sandboxed worker. Syntax errors are located with acorn 8.18.0, MIT. Licence and source
Exit ticket
5 questions, no hints. Score 80% or more to complete the lesson.
Finish every activity above to unlock the exit ticket.