Running Scripts & the REPL
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.
Learn Running Scripts & the REPL in our free Node.js course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Node.js course at LearnCodingFast — hands-on lessons with examples you run in your browser, 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
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 R eads what you type, E valuates it, P rints the result, and L oops 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 ).
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.
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.
Common 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.
Pro 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
Frequently Asked Questions
🎉 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
- ✅ Next lesson: Modules (CommonJS & ESM) — split your code across files
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)