Skip to main content

repl

Deno compatibility

All symbols are not supported.

The node:repl module provides a Read-Eval-Print-Loop (REPL) implementation that is available both as a standalone program or includible in other applications. It can be accessed using:

import repl from 'node:repl';

Usage in Deno

import * as mod from "node:repl";

Functions

f
start
No documentation available

    Type Aliases

    T
    REPLCommandAction
    No documentation available
      T
      REPLEval
      No documentation available
        T
        REPLWriter
        No documentation available

          Variables

          v
          REPL_MODE_SLOPPY

          A flag passed in the REPL options. Evaluates expressions in sloppy mode.

            v
            REPL_MODE_STRICT

            A flag passed in the REPL options. Evaluates expressions in strict mode. This is equivalent to prefacing every repl statement with 'use strict'.

              v
              writer

              This is the default "writer" value, if none is passed in the REPL options, and it can be overridden by custom print functions.



                class REPLServer

                extends Interface

                Usage in Deno

                import { REPLServer } from "node:repl";
                

                Deno compatibility

                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 #

                #REPLServer()
                new

                NOTE: According to the documentation:

                Instances of repl.REPLServer are created using the repl.start() method and should not be created directly using the JavaScript new keyword.

                REPLServer cannot be subclassed due to implementation specifics in NodeJS.

                Properties #

                #commands: ReadOnlyDict<REPLCommand>
                readonly

                The commands registered via replServer.defineCommand().

                Specified in the REPL options, this is the function to use for custom Tab auto-completion.

                #context: Context
                readonly

                The vm.Context provided to the eval function to be used for JavaScript evaluation.

                #editorMode: boolean
                readonly

                A value indicating whether the REPL is currently in "editor mode".

                #eval: REPLEval
                readonly

                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.

                #ignoreUndefined: boolean
                readonly

                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.

                #input: ReadableStream
                readonly

                The Readable stream from which REPL input will be read.

                #inputStream: ReadableStream
                deprecated
                readonly
                #last: any
                readonly

                The last evaluation result from the REPL (assigned to the _ variable inside of the REPL).

                #lastError: any
                readonly

                The last error raised inside the REPL (assigned to the _error variable inside of the REPL).

                #output: WritableStream
                readonly

                The Writable stream to which REPL output will be written.

                #outputStream: WritableStream
                deprecated
                readonly

                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'.
                #underscoreAssigned: boolean
                readonly

                A value indicating whether the _ variable has been assigned.

                #underscoreErrAssigned: boolean
                readonly

                A value indicating whether the _error variable has been assigned.

                #useColors: boolean
                readonly

                Specified in the REPL options, this is a value indicating whether the default writer function should include ANSI color styling to REPL output.

                #useGlobal: boolean
                readonly

                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.

                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 #

                #addListener(
                event: string,
                listener: (...args: any[]) => void,
                ): this

                events.EventEmitter

                1. close - inherited from readline.Interface
                2. line - inherited from readline.Interface
                3. pause - inherited from readline.Interface
                4. resume - inherited from readline.Interface
                5. SIGCONT - inherited from readline.Interface
                6. SIGINT - inherited from readline.Interface
                7. SIGTSTP - inherited from readline.Interface
                8. exit
                9. reset
                #addListener(
                event: "close",
                listener: () => void,
                ): this
                #addListener(
                event: "line",
                listener: (input: string) => void,
                ): this
                #addListener(
                event: "pause",
                listener: () => void,
                ): this
                #addListener(
                event: "resume",
                listener: () => void,
                ): this
                #addListener(
                event: "SIGCONT",
                listener: () => void,
                ): this
                #addListener(
                event: "SIGINT",
                listener: () => void,
                ): this
                #addListener(
                event: "SIGTSTP",
                listener: () => void,
                ): this
                #addListener(
                event: "exit",
                listener: () => void,
                ): this
                #addListener(
                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.

                #defineCommand(
                keyword: string,
                ): 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!
                
                #displayPrompt(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.

                #emit(
                event: string | symbol,
                ...args: any[],
                ): boolean
                #emit(event: "close"): boolean
                #emit(
                event: "line",
                input: string,
                ): boolean
                #emit(event: "pause"): boolean
                #emit(event: "resume"): boolean
                #emit(event: "SIGCONT"): boolean
                #emit(event: "SIGINT"): boolean
                #emit(event: "SIGTSTP"): boolean
                #emit(event: "exit"): boolean
                #emit(
                event: "reset",
                context: Context,
                ): boolean
                #on(
                event: string,
                listener: (...args: any[]) => void,
                ): this
                #on(
                event: "close",
                listener: () => void,
                ): this
                #on(
                event: "line",
                listener: (input: string) => void,
                ): this
                #on(
                event: "pause",
                listener: () => void,
                ): this
                #on(
                event: "resume",
                listener: () => void,
                ): this
                #on(
                event: "SIGCONT",
                listener: () => void,
                ): this
                #on(
                event: "SIGINT",
                listener: () => void,
                ): this
                #on(
                event: "SIGTSTP",
                listener: () => void,
                ): this
                #on(
                event: "exit",
                listener: () => void,
                ): this
                #on(
                event: "reset",
                listener: (context: Context) => void,
                ): this
                #once(
                event: string,
                listener: (...args: any[]) => void,
                ): this
                #once(
                event: "close",
                listener: () => void,
                ): this
                #once(
                event: "line",
                listener: (input: string) => void,
                ): this
                #once(
                event: "pause",
                listener: () => void,
                ): this
                #once(
                event: "resume",
                listener: () => void,
                ): this
                #once(
                event: "SIGCONT",
                listener: () => void,
                ): this
                #once(
                event: "SIGINT",
                listener: () => void,
                ): this
                #once(
                event: "SIGTSTP",
                listener: () => void,
                ): this
                #once(
                event: "exit",
                listener: () => void,
                ): this
                #once(
                event: "reset",
                listener: (context: Context) => void,
                ): this
                #prependListener(
                event: string,
                listener: (...args: any[]) => void,
                ): this
                #prependListener(
                event: "close",
                listener: () => void,
                ): this
                #prependListener(
                event: "line",
                listener: (input: string) => void,
                ): this
                #prependListener(
                event: "pause",
                listener: () => void,
                ): this
                #prependListener(
                event: "resume",
                listener: () => void,
                ): this
                #prependListener(
                event: "SIGCONT",
                listener: () => void,
                ): this
                #prependListener(
                event: "SIGINT",
                listener: () => void,
                ): this
                #prependListener(
                event: "SIGTSTP",
                listener: () => void,
                ): this
                #prependListener(
                event: "exit",
                listener: () => void,
                ): this
                #prependListener(
                event: "reset",
                listener: (context: Context) => void,
                ): this
                #prependOnceListener(
                event: string,
                listener: (...args: any[]) => void,
                ): this
                #prependOnceListener(
                event: "close",
                listener: () => void,
                ): this
                #prependOnceListener(
                event: "line",
                listener: (input: string) => void,
                ): this
                #prependOnceListener(
                event: "pause",
                listener: () => void,
                ): this
                #prependOnceListener(
                event: "resume",
                listener: () => void,
                ): this
                #prependOnceListener(
                event: "SIGCONT",
                listener: () => void,
                ): this
                #prependOnceListener(
                event: "SIGINT",
                listener: () => void,
                ): this
                #prependOnceListener(
                event: "SIGTSTP",
                listener: () => void,
                ): this
                #prependOnceListener(
                event: "exit",
                listener: () => void,
                ): this
                #prependOnceListener(
                event: "reset",
                listener: (context: Context) => void,
                ): this
                #setupHistory(
                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.


                function start

                Usage in Deno

                import { start } from "node:repl";
                
                #start(options?: string | ReplOptions): REPLServer

                Deno compatibility

                This symbol is not supported.

                The repl.start() method creates and starts a REPLServer instance.

                If options is a string, then it specifies the input prompt:

                import repl from 'node:repl';
                
                // a Unix style prompt
                repl.start('$ ');
                

                Parameters #

                #options: string | ReplOptions
                optional

                Return Type #


                interface REPLCommand

                Usage in Deno

                import { type REPLCommand } from "node:repl";
                

                Deno compatibility

                This symbol is not supported.

                Properties #

                #help: string | undefined
                optional

                Help text to be displayed when .help is entered.

                The function to execute, optionally accepting a single string argument.


                interface ReplOptions

                Usage in Deno

                import { type ReplOptions } from "node:repl";
                

                Deno compatibility

                This symbol is not supported.

                Properties #

                #prompt: string | undefined
                optional

                The input prompt to display.

                #input: ReadableStream | undefined
                optional

                The Readable stream from which REPL input will be read.

                #output: WritableStream | undefined
                optional

                The Writable stream to which REPL output will be written.

                #terminal: boolean | undefined
                optional

                If true, specifies that the output should be treated as a TTY terminal, and have ANSI/VT100 escape codes written to it. Default: checking the value of the isTTY property on the output stream upon instantiation.

                #eval: REPLEval | undefined
                optional

                The function to be used when evaluating each given line of input. Default: an async wrapper for the JavaScript eval() function. An eval function can error with repl.Recoverable to indicate the input was incomplete and prompt for additional lines.

                #preview: boolean | undefined
                optional

                Defines if the repl prints output previews or not.

                #useColors: boolean | undefined
                optional

                If true, specifies that the default writer function should include ANSI color styling to REPL output. If a custom writer function is provided then this has no effect.

                #useGlobal: boolean | undefined
                optional

                If true, specifies that the default evaluation function will use the JavaScript global as the context as opposed to creating a new separate context for the REPL instance. The node CLI REPL sets this value to true.

                #ignoreUndefined: boolean | undefined
                optional

                If true, specifies that the default writer will not output the return value of a command if it evaluates to undefined.

                #writer: REPLWriter | undefined
                optional

                The function to invoke to format the output of each command before writing to output.

                #completer:
                optional

                An optional function used for custom Tab auto completion.

                A flag that specifies whether the default evaluator executes all JavaScript commands in strict mode or default (sloppy) mode. Accepted 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'.
                #breakEvalOnSigint: boolean | undefined
                optional

                Stop evaluating the current piece of code when SIGINT is received, i.e. Ctrl+C is pressed. This cannot be used together with a custom eval function.


                type alias REPLCommandAction

                Usage in Deno

                import { type REPLCommandAction } from "node:repl";
                

                Deno compatibility

                This symbol is not supported.

                Definition #

                (
                this: REPLServer,
                text: string,
                ) => void

                type alias REPLEval

                Usage in Deno

                import { type REPLEval } from "node:repl";
                

                Deno compatibility

                This symbol is not supported.

                Definition #

                (
                this: REPLServer,
                evalCmd: string,
                context: Context,
                file: string,
                cb: (
                err: Error | null,
                result: any,
                ) => void
                ,
                ) => void

                type alias REPLWriter

                Usage in Deno

                import { type REPLWriter } from "node:repl";
                

                Deno compatibility

                This symbol is not supported.

                Definition #

                (
                this: REPLServer,
                obj: any,
                ) => string

                variable REPL_MODE_SLOPPY

                Usage in Deno

                import { REPL_MODE_SLOPPY } from "node:repl";
                

                A flag passed in the REPL options. Evaluates expressions in sloppy mode.

                Type #

                unique symbol

                variable REPL_MODE_STRICT

                Usage in Deno

                import { REPL_MODE_STRICT } from "node:repl";
                

                A flag passed in the REPL options. Evaluates expressions in strict mode. This is equivalent to prefacing every repl statement with 'use strict'.

                Type #

                unique symbol

                variable writer

                Usage in Deno

                import { writer } from "node:repl";
                

                This is the default "writer" value, if none is passed in the REPL options, and it can be overridden by custom print functions.

                Type #

                REPLWriter & { options: InspectOptions; }

                Did you find what you needed?

                Privacy policy