The REPL Module in Node.js

The repl module in Node.js provides a Read-Eval-Print-Loop (REPL) environment that can be used as a standalone program or embedded within applications. It allows developers to interactively execute JavaScript code, inspect results, and experiment with Node.js features. REPL supports input completion, history, multi-line editing, ANSI-styled output, error recovery, and custom evaluation functions.

repl.REPLServerSpecial commands (.break, .clear, .exit, .help, .save, .load, .editor)Events: exit, resetContext and global variables

~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 domain module.
  • The special variable _ stores the last evaluated result.
  • _error stores 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

Related Articles

Comprehensive Guide to the Node.js VM Module (node:vm)

The node:vm module allows you to compile and execute JavaScript code inside isolated V8 contexts — essentially creating lightweight sandboxes within your Node.js application. These contexts have their own global scope and can run code independently from the main environment. However, vm is NOT a security sandbox. It is powerful for dynamic code execution, template engines, REPLs, plugin systems, and controlled module execution, but it must never be used to run untrusted code.

Continue

Comprehensive Guide to the V8 Module in Node.js (node:v8)

The node:v8 module exposes low-level APIs that interact directly with the V8 JavaScript engine embedded in Node.js. . These APIs provide access to heap statistics, heap snapshots, coverage tools, serialization mechanisms, V8 flags, object queries, and promise lifecycle hooks. The module is essential for performance analysis, memory debugging, tooling, and advanced Node.js internals work.

Continue

Comprehensive Guide to Node.js Worker Threads (node:worker_threads)

The node:worker_threads module enables true multithreading in Node.js by running JavaScript in separate threads. While Node.js is traditionally single‑threaded, worker threads allow CPU‑intensive tasks to run in parallel without blocking the event loop. They support shared memory, zero‑copy transfers, worker pools, resource limits, and advanced synchronization APIs. Worker threads are ideal for heavy computation, data processing, and parallel workloads—while async I/O remains best handled by the main thread.

Continue

Comprehensive Guide to the Node.js util Module (node:util)

The node:util module provides a powerful collection of helper functions used throughout Node.js core and extremely useful for application developers. These utilities support debugging, inspection, formatting, type checking, callback/Promise conversions, argument parsing, text encoding, MIME handling, and more. It is one of the most versatile and essential toolkits in the Node.js ecosystem.

Continue

Comprehensive Guide to the URL Module in Node.js

The node:url module provides tools for parsing, constructing, and manipulating URLs. Node.js supports two URL APIs: WHATWG URL API — modern, browser‑compatible, standards‑based. Legacy Node.js URL API — older, Node‑specific, now discouraged. The WHATWG API is the recommended approach for all modern applications. It provides a clean, consistent interface for working with URL components, query parameters, and structured URL patterns.

Continue

راهنمای جامع UDP / Datagram Sockets در Node.js

ماژول node:dgram پیاده‌سازی کامل سوکت‌های UDP را در Node.js فراهم می‌کند. UDP یک پروتکل سبک، بدون اتصال (connectionless) و مناسب برای برنامه‌های بلادرنگ مانند VoIP، بازی‌ها، IoT، سیستم‌های پخش (broadcast) و چندپخشی (multicast) است. این ماژول امکان ساخت سوکت، ارسال و دریافت دیتاگرام، مدیریت TTL، عضویت در گروه‌های multicast، کنترل بافرها، و مدیریت رفتار سطح پایین شبکه را فراهم می‌کند.

Continue