This symbol is not supported.
Instances of repl.REPLServer
are created using the 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);
Constructors Jump to heading
Jump to headingREPLServer()
NOTE: According to the documentation:
Instances of
repl.REPLServer
are created using therepl.start()
method and should not be created directly using the JavaScriptnew
keyword.
REPLServer
cannot be subclassed due to implementation specifics in NodeJS.
Properties Jump to heading
Jump to headingcommands: ReadOnlyDict<REPLCommand>
The commands registered via replServer.defineCommand()
.
Jump to headingcompleter: Completer | AsyncCompleter
Specified in the REPL options, this is the function to use for custom Tab auto-completion.
Jump to headingcontext: Context
The vm.Context
provided to the eval
function to be used for JavaScript
evaluation.
Jump to headingeditorMode: boolean
A value indicating whether the REPL is currently in "editor mode".
Jump to headingeval: REPLEval
Specified in the REPL options, this is the function to be used when evaluating each
given line of input. If not specified in the REPL options, this is an async wrapper
for the JavaScript eval()
function.
Jump to headingignoreUndefined: boolean
Specified in the REPL options, this is a value indicating whether the default writer
function should output the result of a command if it evaluates to undefined
.
Jump to headinginput: ReadableStream
The Readable
stream from which REPL input will be read.
Jump to headinginputStream: ReadableStream
Jump to headinglast: any
The last evaluation result from the REPL (assigned to the _
variable inside of the REPL).
Jump to headinglastError: any
The last error raised inside the REPL (assigned to the _error
variable inside of the REPL).
Jump to headingoutput: WritableStream
The Writable
stream to which REPL output will be written.
Jump to headingoutputStream: WritableStream
Specified in the REPL options, this is a flag that specifies whether the default eval
function should execute all JavaScript commands in strict mode or default (sloppy) mode.
Possible values are:
repl.REPL_MODE_SLOPPY
- evaluates expressions in sloppy mode.repl.REPL_MODE_STRICT
- evaluates expressions in strict mode. This is equivalent to prefacing every repl statement with'use strict'
.
Jump to headingunderscoreAssigned: boolean
A value indicating whether the _
variable has been assigned.
Jump to headingunderscoreErrAssigned: boolean
A value indicating whether the _error
variable has been assigned.
Jump to headinguseColors: boolean
Specified in the REPL options, this is a value indicating whether the default
writer
function should include ANSI color styling to REPL output.
Jump to headinguseGlobal: boolean
Specified in the REPL options, this is a value indicating whether the default eval
function will use the JavaScript global
as the context as opposed to creating a new
separate context for the REPL instance.
Jump to headingwriter: REPLWriter
Specified in the REPL options, this is the function to invoke to format the output of
each command before writing to outputStream
. If not specified in the REPL options,
this will be a wrapper for util.inspect
.
Methods Jump to heading
Jump to headingaddListener(event: string,listener: (...args: any[]) => void,): this
events.EventEmitter
- close - inherited from
readline.Interface
- line - inherited from
readline.Interface
- pause - inherited from
readline.Interface
- resume - inherited from
readline.Interface
- SIGCONT - inherited from
readline.Interface
- SIGINT - inherited from
readline.Interface
- SIGTSTP - inherited from
readline.Interface
- exit
- reset
Jump to headingaddListener(event: "close",listener: () => void,): this
Jump to headingaddListener(event: "line",listener: (input: string) => void,): this
Jump to headingaddListener(event: "pause",listener: () => void,): this
Jump to headingaddListener(event: "resume",listener: () => void,): this
Jump to headingaddListener(event: "SIGCONT",listener: () => void,): this
Jump to headingaddListener(event: "SIGINT",listener: () => void,): this
Jump to headingaddListener(event: "SIGTSTP",listener: () => void,): this
Jump to headingaddListener(event: "exit",listener: () => void,): this
Jump to headingaddListener(event: "reset",listener: (context: Context) => void,): this
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.
Jump to headingdefineCommand(keyword: string,cmd: REPLCommandAction | REPLCommand,): 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();
});
The new commands can then be used from within the REPL instance:
> .sayhello Node.js User
Hello, Node.js User!
> .saybye
Goodbye!
Jump to headingdisplayPrompt(preserveCursor?: boolean): void
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.
When multi-line input is being entered, an ellipsis 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.
Jump to headingemit(event: string | symbol,...args: any[],): boolean
Jump to headingemit(event: "close"): boolean
Jump to headingemit(event: "line",input: string,): boolean
Jump to headingemit(event: "pause"): boolean
Jump to headingemit(event: "resume"): boolean
Jump to headingemit(event: "SIGCONT"): boolean
Jump to headingemit(event: "SIGINT"): boolean
Jump to headingemit(event: "SIGTSTP"): boolean
Jump to headingemit(event: "exit"): boolean
Jump to headingemit(event: "reset",context: Context,): boolean
Jump to headingon(event: string,listener: (...args: any[]) => void,): this
Jump to headingon(event: "close",listener: () => void,): this
Jump to headingon(event: "line",listener: (input: string) => void,): this
Jump to headingon(event: "pause",listener: () => void,): this
Jump to headingon(event: "resume",listener: () => void,): this
Jump to headingon(event: "SIGCONT",listener: () => void,): this
Jump to headingon(event: "SIGINT",listener: () => void,): this
Jump to headingon(event: "SIGTSTP",listener: () => void,): this
Jump to headingon(event: "exit",listener: () => void,): this
Jump to headingon(event: "reset",listener: (context: Context) => void,): this
Jump to headingonce(event: string,listener: (...args: any[]) => void,): this
Jump to headingonce(event: "close",listener: () => void,): this
Jump to headingonce(event: "line",listener: (input: string) => void,): this
Jump to headingonce(event: "pause",listener: () => void,): this
Jump to headingonce(event: "resume",listener: () => void,): this
Jump to headingonce(event: "SIGCONT",listener: () => void,): this
Jump to headingonce(event: "SIGINT",listener: () => void,): this
Jump to headingonce(event: "SIGTSTP",listener: () => void,): this
Jump to headingonce(event: "exit",listener: () => void,): this
Jump to headingonce(event: "reset",listener: (context: Context) => void,): this
Jump to headingprependListener(event: string,listener: (...args: any[]) => void,): this
Jump to headingprependListener(event: "close",listener: () => void,): this
Jump to headingprependListener(event: "line",listener: (input: string) => void,): this
Jump to headingprependListener(event: "pause",listener: () => void,): this
Jump to headingprependListener(event: "resume",listener: () => void,): this
Jump to headingprependListener(event: "SIGCONT",listener: () => void,): this
Jump to headingprependListener(event: "SIGINT",listener: () => void,): this
Jump to headingprependListener(event: "SIGTSTP",listener: () => void,): this
Jump to headingprependListener(event: "exit",listener: () => void,): this
Jump to headingprependListener(event: "reset",listener: (context: Context) => void,): this
Jump to headingprependOnceListener(event: string,listener: (...args: any[]) => void,): this
Jump to headingprependOnceListener(event: "close",listener: () => void,): this
Jump to headingprependOnceListener(event: "line",listener: (input: string) => void,): this
Jump to headingprependOnceListener(event: "pause",listener: () => void,): this
Jump to headingprependOnceListener(event: "resume",listener: () => void,): this
Jump to headingprependOnceListener(event: "SIGCONT",listener: () => void,): this
Jump to headingprependOnceListener(event: "SIGINT",listener: () => void,): this
Jump to headingprependOnceListener(event: "SIGTSTP",listener: () => void,): this
Jump to headingprependOnceListener(event: "exit",listener: () => void,): this
Jump to headingprependOnceListener(event: "reset",listener: (context: Context) => void,): this
Jump to headingsetupHistory(path: string,callback: (err: Error | null,repl: this,) => void,): void
Initializes 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.