Working with Node.js REPL

1. Starting REPL Session

CommandPurpose
nodeStart REPL
node -i -e "code"Run code then drop into REPL
NODE_REPL_HISTORY=~/.node_historyPersist history across sessions

2. Evaluating JavaScript Expressions

InputResult
1 + 23
const x = 5undefined (declaration)
await fetch("https://api.example.com")Top-level await supported v18+

3. Using Multi-Line Input (.editor)

Example: Editor mode

> .editor
// Entering editor mode (Ctrl+D to finish, Ctrl+C to cancel)
function add(a, b) {
  return a + b;
}
^D
> add(2, 3)
5
KeyAction
Ctrl+DFinish input
Ctrl+CCancel

4. Navigating REPL History

KeyAction
↑ / ↓Previous / next command
Ctrl+RReverse history search
NODE_REPL_HISTORY_SIZENumber of entries kept

5. Using Tab Completion

ActionBehavior
Math.<Tab>List all properties of Math
req<Tab>Complete identifier name
./fil<Tab>Path completion in require

6. Using Underscore for Last Result (_)

Example: Underscore variables

> 2 + 3
5
> _ * 2
10
> throw new Error("oops")
> _error.message
"oops"
VariableHolds
_Last evaluated value
_errorLast uncaught error

7. Inspecting Objects in REPL

SettingEffect
util.inspect.defaultOptions.depth = nullUnlimited depth
util.inspect(obj, { colors: true })Colorized output
console.dir(obj, { depth: 4 })Custom depth

8. Loading External Files (.load)

Example: Load script into REPL

> .load ./helpers.js
// All top-level declarations now available
CommandAction
.load <file>Read file and execute line by line

9. Saving REPL Session (.save)

CommandAction
.save session.jsWrite history to file
.load session.jsReplay later

10. Clearing REPL Context (.clear)

CommandAction
.clearReset REPL context (alias for .break in older versions)

11. Using REPL Commands (.help, .exit, .break)

CommandPurpose
.helpList all REPL commands
.exit / Ctrl+DQuit REPL
.break / Ctrl+CAbort current multi-line input
.editorMulti-line editor mode