The Node.js REPL
REPL stands for Read–Eval–Print–Loop: it reads what you type, evaluates it, prints the result, and loops. It is an interactive JavaScript playground built into Node — perfect for trying an API, checking a regex, or doing quick math without creating a file. It is the same engine that powers node with a script, just driven line by line.
Starting the REPL
Just run node with no arguments:
node
Welcome to Node.js v20.11.0. Type ".help" for more information. >
The > prompt is waiting for input. Type an expression and press Enter:
> 2 + 2
4
> const name = 'Node'
undefined
> `Hello, ${name}!`
'Hello, Node!'
> [1, 2, 3].map((n) => n * 2)
[ 2, 4, 6 ]The _ and _error specials
The REPL keeps the last result in _ and the last thrown error in _error — handy for chaining quick experiments:
> 2 + 2 4 > _ * 10 // _ is the previous result 40 > _ + 5 45
Handy features
_holds the result of the last expression;_errorholds the last error.Tab completion — type
console.then press Tab to list every method.Multi-line — unclosed brackets/functions continue onto the next line, shown with a
...prompt.Top-level await — you can
awaita promise directly, no wrapperasyncfunction needed.Up/Down arrows — scroll through command history (persisted across sessions, see below).
REPL dot-commands
Command | What it does |
|---|---|
| List all dot-commands |
| Quit the REPL (or press Ctrl+D) |
| Reset the REPL context (wipe defined variables) |
| Save everything typed this session to a file |
| Read and evaluate a file in the current REPL |
| Enter multi-line editor mode (Ctrl+D to run) |
Persistent history and presets
The REPL is a real program too
Core modules are available, and recent Node auto-loads many of them lazily — typing fs or crypto just works without require. You can even embed a REPL inside your own app for live debugging:
import repl from 'node:repl'
const r = repl.start('app> ')
r.context.db = myDatabase // expose objects into the REPL sessionWhen to use the REPL (and when not)
The REPL is great for exploration — "what does Array.from do again?", "does this regex match?". For anything you want to keep, save it to a .js file and run it as a script. For a one-off expression without entering the loop, use node -e (evaluate) or node -p (evaluate and print):
node -e "console.log(Math.max(3, 9, 1))" # -e: run it node -p "Math.max(3, 9, 1)" # -p: run it AND print the result
9 9