~2 min read • Updated Dec 30, 2025
1. Introduction
The repl module is loaded with require('node:repl'). It exports the REPLServer class, which reads user input, evaluates it, and prints the result. Input and output can be connected to stdin and stdout or other streams.
2. Features
- Automatic input completion.
- Emacs-style line editing.
- Multi-line input support.
- ZSH-like reverse-i-search and substring-based history search.
- ANSI-styled output.
- Persistent session history.
3. Special Commands
.break: Abort multi-line input..clear: Reset REPL context..exit: Exit the REPL..help: Show available commands..save: Save session to a file..load: Load a file into the session..editor: Enter editor mode.
4. Default Evaluation
By default, REPL evaluates JavaScript expressions and provides access to Node.js core modules. Variables declared are global unless scoped within blocks or functions.
5. Context and Variables
Developers can expose variables to REPL by assigning them to the context object. Properties can be made read-only using Object.defineProperty().
6. Error Handling
- Uncaught exceptions are managed using the
domainmodule. - The special variable
_stores the last evaluated result. _errorstores the last error.
7. Await Support
Top-level await is supported in REPL, allowing asynchronous code execution directly. However, redeclaring constants after using await may cause errors.
8. Custom Commands
New commands can be defined using defineCommand():
replServer.defineCommand('sayhello', {
help: 'Say hello',
action(name) {
console.log(`Hello, ${name}!`);
},
});
9. Events
- exit: Triggered when REPL exits.
- reset: Triggered when REPL context is cleared.
10. History and Environment Variables
REPL saves input history to .node_repl_history. Environment variables like NODE_REPL_HISTORY and NODE_REPL_HISTORY_SIZE control persistence and size.
11. Advanced Examples
- Running REPL over TCP or Unix sockets.
- Embedding REPL in HTTP servers (e.g., accessible via curl).
Conclusion
The repl module is a powerful tool for interactive development and debugging in Node.js. With features like auto-completion, history, top-level await, and custom commands, REPL provides a flexible environment for experimenting with code and managing live applications.
Written & researched by Dr. Shahin Siami