class REPLServer extends readline.Interface
Instances of repl.REPLServer are created using the repl.start() method
or directly using the JavaScript new keyword.
import repl from 'node:repl'; const options = { useColors: true }; const firstInstance = repl.start(options); const secondInstance = new repl.REPLServer(options);
const repl = require('node:repl'); const options = { useColors: true }; const firstInstance = repl.start(options); const secondInstance = new repl.REPLServer(options);
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. The listener
callback is invoked without any arguments.
replServer.on('exit', () => { console.log('Received "exit" event from repl!'); process.exit(); });
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. The listener callback will be called with a
reference to the context object as the only argument.
This can be used primarily to re-initialize REPL context to some pre-defined state:
import repl from 'node:repl'; function initializeContext(context) { context.m = 'test'; } const r = repl.start({ prompt: '> ' }); initializeContext(r.context); r.on('reset', initializeContext);
const repl = require('node:repl'); function initializeContext(context) { context.m = 'test'; } const r = repl.start({ prompt: '> ' }); initializeContext(r.context); r.on('reset', initializeContext);
When this code is executed, the global 'm' variable can be modified but then
reset to its initial value using the .clear command:
$ ./node example.js > m 'test' > m = 1 1 > m 1 > .clear Clearing context... > m 'test' >
replServer.defineCommand(keyword, cmd): void
The replServer.defineCommand() method is used to add new .-prefixed commands
to the REPL instance. Such commands are invoked by typing a . followed by the
keyword. The cmd is either a Function or an Object with the following
properties:
The following example shows two new commands added to the REPL instance:
import repl from 'node:repl'; const replServer = repl.start({ prompt: '> ' }); replServer.defineCommand('sayhello', { help: 'Say hello', action(name) { this.clearBufferedCommand(); console.log(`Hello, ${name}!`); this.displayPrompt(); }, }); replServer.defineCommand('saybye', function saybye() { console.log('Goodbye!'); this.close(); });
const repl = require('node:repl'); const replServer = repl.start({ prompt: '> ' }); replServer.defineCommand('sayhello', { help: 'Say hello', action(name) { this.clearBufferedCommand(); console.log(`Hello, ${name}!`); this.displayPrompt(); }, }); replServer.defineCommand('saybye', function saybye() { console.log('Goodbye!'); this.close(); });
The new commands can then be used from within the REPL instance:
> .sayhello Node.js User Hello, Node.js User! > .saybye Goodbye!
replServer.displayPrompt(preserveCursor?): void
booleanThe 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.
When multi-line input is being entered, a pipe '|' is printed rather than the
'prompt'.
When preserveCursor is true, the cursor placement will not be reset to 0.
The replServer.displayPrompt method is primarily intended to be called from
within the action function for commands registered using the
replServer.defineCommand() method.
replServer.clearBufferedCommand(): void
The replServer.clearBufferedCommand() method clears any command that has been
buffered but not yet executed. This method is primarily intended to be
called from within the action function for commands registered using the
replServer.defineCommand() method.
replServer.setupHistory(historyConfig, callback): void
stringnumber0. This option makes sense only if
terminal is set to true by the user or by an internal output check,
otherwise the history caching mechanism is not initialized at all.
Default: 30.booleantrue, when a new input line added
to the history list duplicates an older one, this removes the older line
from the list. Default: false.FunctionErrorrepl.REPLServerFunctiononHistoryFileLoaded in historyConfig)Errorrepl.REPLServerInitializes a history log file for the REPL instance. When executing the Node.js binary and using the command-line REPL, a history file is initialized by default. However, this is not the case when creating a REPL programmatically. Use this method to initialize a history log file when working with REPL instances programmatically.