Running Scripts & the REPL
Reviewed & published by Brayan K
The Node.js REPL (Read-Eval-Print Loop) is an interactive prompt that runs JavaScript one line at a time, while the node command also runs whole .js script files from your terminal.
Part of the free Node.js course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.
By the end of this lesson you'll know how to open the REPL and experiment in it, how to write a JavaScript file and run it with node app.js, and a handful of command-line flags that make Node faster to use day to day.
What You'll Learn in This Lesson
- How to start the REPL by typing node with no file name
- How the REPL evaluates expressions and auto-prints results
- What the special _ variable does and how to use it
- How to write a .js file and run it with node app.js
- Why scripts stay silent unless you console.log
- Handy flags: node -v, node -e, node --check, node --watch
1️⃣ The Interactive REPL
Type node on its own — with no file name — and you drop into the REPL. The name spells out exactly what it does in a loop: it Reads what you type, Evaluates it, Prints the result, and Loops back for more. You'll see a > prompt; type any JavaScript expression and press Enter to see its value instantly.
A few things make the REPL pleasant to live in. The special variable _ always holds the last result, so you can build on what you just computed. Commands that start with a dot are REPL controls rather than JavaScript: .help lists them all, and .exit quits (so does Ctrl+D).
# Type "node" with no file name to open the interactive REPL.
$ node
Welcome to Node.js v20.11.0.
Type ".help" for more information.
# Now you're at the > prompt. Type any JavaScript expression
# and press Enter — Node evaluates it and PRINTS the result.
> 2 + 2
4
> "hello".toUpperCase()
'HELLO'
> [1, 2, 3].map(n => n * 10)
[ 10, 20, 30 ]
# The special _ variable always holds the LAST result.
> 10 * 5
50
> _ + 1
51
# .help lists the REPL's dot-commands. .exit (or Ctrl+D) quits.
> .help
.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.editor Enter editor mode
.exit Exit the REPL
.help Print this help message
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
> .exit
$2️⃣ Writing and Running a Script File
The REPL is great for quick experiments, but real programs live in files. Save your JavaScript in a file ending in .js — say app.js — and Node runs the whole thing top to bottom. Read this worked example first; every line is explained.
// Save this in a file called app.js
const name = "Ada";
const year = 2026;
// Template literals use backticks and ${...} to insert values.
console.log(`Hello, ${name}!`);
console.log(`The year is ${year}.`);
// console.log can take several values; it joins them with spaces.
console.log("2 + 2 =", 2 + 2);Now run it. In your terminal, cd into the folder that contains app.js and type node app.js. One important difference from the REPL: a script does not auto-print expression results. Only what you pass to console.log appears — everything else runs silently.
3️⃣ Useful Command-Line Flags
The node command takes flags — small options that change what it does. Four of them earn their place early on: check the version, evaluate a one-liner, verify syntax without running, and re-run a file automatically on every save.
🎯 Your Turn
The program below works once you fill in the three blanks marked ___. Follow the 👉 hints, save it as greet.js, run it with node greet.js, and compare with the expected output.
// 🎯 YOUR TURN — save this as greet.js, fill each ___, then run:
// node greet.js
// 1) Store your name in a variable
const name = ___; // 👉 your name in "double quotes", e.g. "Sam"
// 2) Print a greeting using a template literal (backticks + ${...})
console.log(`Hi, ${___}!`); // 👉 put the variable name inside the ${ }
// 3) Print the result of a little math, labelled
console.log("3 * 4 =", ___); // 👉 the expression 3 * 4
// ✅ Expected output (with name = "Sam"):
// Hi, Sam!
// 3 * 4 = 12Common Errors (and the fix)
- "Cannot find module '/path/app.js'" — Node can't find the file you named. You're usually in the wrong folder, or the name is misspelled. cd into the folder that holds the file (use ls or dir to confirm it's there) and run node app.js again.
- Forgetting quotes in node -e — The code after -e must be a single quoted string. Without quotes your shell mangles the parentheses and you get a confusing error. Write node -e "console.log(2+2)".
- "SyntaxError: ..." — Node parsed your file but the JavaScript is malformed (a missing bracket, an unclosed string, a stray comma). Run node --check app.js to find syntax problems without running the program.
- Stuck at the > prompt — You're inside the REPL and your normal terminal commands don't work. That's expected: the REPL only runs JavaScript. Type .exit or press Ctrl+D to get back to your shell.
- A lonely ... prompt that won't go away — The REPL thinks your expression isn't finished (an open bracket or quote). Press Ctrl+C once, or type .break, to cancel and return to a fresh > prompt.
Mini-Challenge: An "About Me" Script
No blanks this time — just a brief and an outline to keep you on track. Write it yourself, save it as about.js, run it with node about.js, and check your output against the example in the comments.
// 🎯 MINI-CHALLENGE: save this as about.js and run it with node.
// No blanks — just a brief and an outline. Write it yourself.
//
// 1. Make a variable for your name and one for your favourite tool.
// 2. Use console.log with a template literal to print one sentence
// that includes BOTH variables.
// 3. On a second line, print how many letters are in your name
// (hint: a string has a .length property).
//
// ✅ Example output (name = "Alex", tool = "Node"):
// Alex builds things with Node.
// My name has 4 letters.
// your code herePro Tips
- 💡 Use the REPL as a calculator: need a quick sum or to test what a string method returns? Type node, try it, and .exit — faster than opening a file.
- 💡 Let --watch save you keystrokes: run node --watch app.js while editing and it re-runs on every save — no more re-typing the command.
- 💡 Press the up arrow in the REPL or terminal to recall your previous command instead of retyping it.
📋 Quick Reference — REPL & Running Scripts
| Task | Command |
|---|---|
| Start the REPL | node |
| Exit the REPL | .exit (or Ctrl+D) |
| Run a file | node app.js |
| Evaluate inline code | node -e "..." |
| Check the version | node -v |
| Check syntax only | node --check app.js |
| Re-run on save (watch) | node --watch app.js |
🎉 Lesson Complete!
- ✅ Typing node with no file opens the interactive REPL
- ✅ The REPL auto-prints each result; _ holds the last one
- ✅ .help lists dot-commands; .exit or Ctrl+D quits
- ✅ node app.js runs a whole file — but only console.log shows output
- ✅ Flags: node -v, node -e "...", node --check, and node --watch
Practice quiz
How do you start the Node.js REPL?
- Type node with no file name
- Type node --repl-start
- Run node repl.js
- Type repl
Answer: Type node with no file name. Typing node on its own, with no file argument, drops you into the interactive REPL.
What does REPL stand for?
- Run-Eval-Print-Log
- Read-Eval-Print-Loop
- Read-Execute-Push-Load
- Run-Export-Print-Loop
Answer: Read-Eval-Print-Loop. REPL is Read, Eval, Print, Loop: it reads input, evaluates it, prints the result, and loops.
What does the special _ variable hold in the REPL?
- The current file name
- The node version
- The result of the last evaluated expression
- The list of dot-commands
Answer: The result of the last evaluated expression. In the REPL, _ always stores the value of the most recent expression.
How do you exit the REPL?
- Type quit()
- Press the spacebar
- Type stop
- Type .exit or press Ctrl+D
Answer: Type .exit or press Ctrl+D. .exit or Ctrl+D leaves the REPL and returns you to your shell.
What command runs a whole JavaScript file named app.js?
- node app.js
- run app.js
- node --file app.js
- exec app.js
Answer: node app.js. node app.js executes the file top to bottom.
Unlike the REPL, what does a script file do with expression results?
- Auto-prints them
- It does not auto-print; only console.log shows output
- Saves them to a file
- Sends them to a server
Answer: It does not auto-print; only console.log shows output. Scripts stay silent unless you explicitly console.log; they do not auto-print like the REPL.
What does the node -e flag do?
- Prints the version
- Edits a file
- Evaluates a snippet of code passed as a string
- Enables ES modules
Answer: Evaluates a snippet of code passed as a string. node -e "..." runs the code in the quoted string without needing a file.
Which flag re-runs a file automatically every time you save it?
- node -v
- node --check
- node -e
- node --watch
Answer: node --watch. node --watch app.js re-runs the file on every save until you press Ctrl+C.
What does node --check app.js do?
- Parses the file for syntax errors without running it
- Runs the file twice
- Installs dependencies
- Prints the file contents
Answer: Parses the file for syntax errors without running it. --check verifies syntax and exits 0 if valid, without executing the program.
Which flag prints the installed Node.js version and exits?
- node --watch
- node -v
- node -e
- node --check
Answer: node -v. node -v (or --version) prints the installed version, such as v20.11.0.
Continue this course
- Previous: Introduction to Node.js
- Next: Modules (CommonJS & ESM) — Split code across files with require/module.exports and import/export
- Quick reference: Node.js cheat sheet
Frequently asked questions
What is the difference between the Node.js REPL and running a script?
The REPL is an interactive prompt: you type one line of JavaScript, press Enter, and Node immediately evaluates it and prints the result — perfect for experimenting and testing small ideas. Running a script (node app.js) executes a whole .js file from top to bottom in one go. A key difference is that the REPL auto-prints the value of each expression, while a script stays silent unless you explicitly call console.log, so scripts always end with real output statements.
How do I exit the Node.js REPL?
Type .exit and press Enter, or press Ctrl+D once. You can also press Ctrl+C twice. On some systems a single Ctrl+C only clears the current line and prints a hint like "(To exit, press Ctrl+C again or Ctrl+D)" — that's the REPL reminding you how to leave. If you ever feel stuck mid-expression, type .break (or press Ctrl+C once) to cancel the current input and get a fresh > prompt.
What does the underscore (_) do in the REPL?
In the REPL, the special variable _ always holds the result of the last evaluated expression. So if you type 10 * 5 and get 50, then typing _ + 1 gives 51. It's a handy shortcut while experimenting. Note this is REPL-only behaviour — _ is not a special variable in regular .js script files, where you'd use a named variable instead.
When should I use node -e instead of a script file?
Use node -e "..." for quick throwaway one-liners you don't want to save to disk — for example node -e "console.log(Date.now())" to print a timestamp. For anything you'll run more than once, want to edit, or need across multiple lines, save it in a .js file and run it with node app.js instead. Scripts are easier to read, reuse, and put under version control.