NodeJSThe Node.js REPL

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:

Bash
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:

JS
> 2 + 2
4
> const name = 'Node'
undefined
> `Hello, ${name}!`
'Hello, Node!'
> [1, 2, 3].map((n) => n * 2)
[ 2, 4, 6 ]
Why does `const name = ...` print `undefined`?
A variable *declaration* is a statement whose own value is `undefined` — and the REPL prints the value of whatever you typed. The assignment still happened, as the next line proves. An *expression* like `2 + 2` has a value, so you see it.
The _ and _error specials

The REPL keeps the last result in _ and the last thrown error in _error — handy for chaining quick experiments:

JS
> 2 + 2
4
> _ * 10        // _ is the previous result
40
> _ + 5
45
Handy features
  • _ holds the result of the last expression; _error holds 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 await a promise directly, no wrapper async function needed.

  • Up/Down arrows — scroll through command history (persisted across sessions, see below).

REPL dot-commands

Command

What it does

.help

List all dot-commands

.exit

Quit the REPL (or press Ctrl+D)

.clear

Reset the REPL context (wipe defined variables)

.save file.js

Save everything typed this session to a file

.load file.js

Read and evaluate a file in the current REPL

.editor

Enter multi-line editor mode (Ctrl+D to run)

Persistent history and presets
History survives restarts
Your REPL history is saved to `~/.node_repl_history`, so the Up arrow recalls commands from previous sessions. Set `NODE_REPL_HISTORY=""` to disable it (useful if you paste secrets), and `NODE_REPL_HISTORY_SIZE` to change how much is kept.
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:

JS
import repl from 'node:repl'

const r = repl.start('app> ')
r.context.db = myDatabase   // expose objects into the REPL session
When 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):

Bash
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
Next
Move from interactive snippets to real files in [Running Your First Script](/nodejs/running-scripts).