Stability: 2 - Stable
node:repl module provides a Read-Eval-Print-Loop (REPL) implementation that is available both as a standalone program or includible in other applications.
Design and Features
Thenode:repl module exports the repl.REPLServer class. While running, instances of repl.REPLServer will accept individual lines of user input, evaluate those according to a user-defined evaluation function, then output the result.
Instances of repl.REPLServer support:
- Automatic completion of inputs
- Completion preview
- Simplistic Emacs-style line editing
- Multi-line inputs
- ZSH-like reverse-i-search
- ZSH-like substring-based history search
- ANSI-styled output
- Saving and restoring current REPL session state
- Error recovery
- Customizable evaluation functions
Commands and Special Keys
The following special commands are supported by all REPL instances:.break- When in the process of inputting a multi-line expression, enter the.breakcommand (or pressCtrl+C) to abort further input or processing of that expression.clear- Resets the REPLcontextto an empty object and clears any multi-line expression being input.exit- Close the I/O stream, causing the REPL to exit.help- Show this list of special commands.save- Save the current REPL session to a file:> .save ./file/to/save.js.load- Load a file into the current REPL session:> .load ./file/to/load.js.editor- Enter editor mode (Ctrl+Dto finish,Ctrl+Cto cancel)
Key Combinations
The following key combinations have special effects:Ctrl+C- When pressed once, has the same effect as the.breakcommand. When pressed twice on a blank line, has the same effect as the.exitcommandCtrl+D- Has the same effect as the.exitcommandTab- When pressed on a blank line, displays global and local (scope) variables. When pressed while entering other input, displays relevant autocompletion options
Default Evaluation
By default, all instances ofrepl.REPLServer use an evaluation function that evaluates JavaScript expressions and provides access to Node.js built-in modules.
JavaScript Expressions
The default evaluator supports direct evaluation of JavaScript expressions:Global and Local Scope
The default evaluator provides access to any variables that exist in the global scope. It is possible to expose a variable to the REPL explicitly by assigning it to thecontext object associated with each REPLServer:
context object appear as local within the REPL:
Accessing Core Node.js Modules
The default evaluator will automatically load Node.js core modules into the REPL environment when used. For instance, unless otherwise declared as a global or scoped variable, the inputfs will be evaluated on-demand as global.fs = require('node:fs').
Assignment of the _ (underscore) Variable
The default evaluator will, by default, assign the result of the most recently evaluated expression to the special variable _ (underscore). Explicitly setting _ to a value will disable this behavior.
_error will refer to the last seen error, if there was any.
await Keyword
Support for the await keyword is enabled at the top level.
One known limitation of using the
await keyword in the REPL is that it will invalidate the lexical scoping of the const keywords.Starting the REPL
repl.start([options])
The repl.start() method creates and starts a repl.REPLServer instance.
Parameters:
optionspromptThe input prompt to display. Default:'> 'inputTheReadablestream from which REPL input will be read. Default:process.stdinoutputTheWritablestream to which REPL output will be written. Default:process.stdoutterminalIftrue, specifies that theoutputshould be treated as a TTY terminalevalThe function to be used when evaluating each given line of inputuseColorsIftrue, specifies that the defaultwriterfunction should include ANSI color styling to REPL outputuseGlobalIftrue, specifies that the default evaluation function will use the JavaScriptglobalas the context. Default:falseignoreUndefinedIftrue, specifies that the default writer will not output the return value of a command if it evaluates toundefined. Default:falsereplModeA flag that specifies whether the default evaluator executes all JavaScript commands in strict mode or default (sloppy) modebreakEvalOnSigintStop evaluating the current piece of code whenSIGINTis received. Default:false
Class: REPLServer
Instances ofrepl.REPLServer are created using the repl.start() method or directly using the JavaScript new keyword.
Event: ‘exit’
The'exit' event is emitted when the REPL is exited either by receiving the .exit command as input, the user pressing Ctrl+C twice to signal SIGINT, or by pressing Ctrl+D to signal 'end' on the input stream.
Event: ‘reset’
The'reset' event is emitted when the REPL’s context is reset. This occurs whenever the .clear command is received as input unless the REPL is using the default evaluator and the repl.REPLServer instance was created with the useGlobal option set to true.
replServer.defineCommand(keyword, cmd)
The replServer.defineCommand() method is used to add new .-prefixed commands to the REPL instance.
Parameters:
keywordThe command keyword (without a leading.character)cmdThe function to invoke when the command is processedhelpHelp text to be displayed when.helpis entered (Optional)actionThe function to execute, optionally accepting a single string argument
replServer.displayPrompt([preserveCursor])
The replServer.displayPrompt() method readies the REPL instance for input from the user, printing the configured prompt to a new line in the output and resuming the input to accept new input.
The Node.js REPL
Node.js itself uses thenode:repl module to provide its own interactive interface for executing JavaScript. This can be used by executing the Node.js binary without passing any arguments (or by passing the -i argument):
Environment Variable Options
Various behaviors of the Node.js REPL can be customized using the following environment variables:NODE_REPL_HISTORY- When a valid path is given, persistent REPL history will be saved to the specified file rather than.node_repl_historyin the user’s home directoryNODE_REPL_HISTORY_SIZE- Controls how many lines of history will be persisted if history is available. Must be a positive number. Default:1000NODE_REPL_MODE- May be either'sloppy'or'strict'. Default:'sloppy', which will allow non-strict mode code to be run
Persistent History
By default, the Node.js REPL will persist history betweennode REPL sessions by saving inputs to a .node_repl_history file located in the user’s home directory. This can be disabled by setting the environment variable NODE_REPL_HISTORY=''.