{ } qjs-modules

repl

Source: lib/repl.js (pure JS)

An interactive read-eval-print loop with line editing, history, completion and syntax highlighting.

Editing

The edit buffer holds the whole input, several lines included. Enter inserts a newline, indented by the open brackets, while the input is incomplete: an open bracket, template literal or block comment. Otherwise it submits.

KeyAction
Enternewline while the input is incomplete, else submit
Up / Downthe line above or below, on the first or last line the history
Home, ^A / End, ^Estart / end of the current line
^Kkill to the end of the line, at its end the newline
Alt-Backspacewith a bracket mismatch, kill from it to the end (^Y brings it back), else the word before the cursor
^Cdrop the input, on an empty line print how to exit, again there exits
^Don an empty line exit, in editor mode submit
^Lclear the screen
^R / ^Ssearch the history backward / forward

Brackets while typing:

  • the innermost opener still open around the cursor is underlined
  • right after a closer, the closer and its opener are shown inverse
  • from the first mismatched closer (a) or (]) to the end, the input is shown on a red background, and Enter submits it instead of waiting for the missing bracket

Pasted text arrives as one piece (bracketed paste), so a multi-line paste is edited as a whole and runs on Enter.

Node.js behavior

  • an error prints as Uncaught TypeError: message, followed by the stack frames below the REPL input, a thrown value as Uncaught 7
  • _ is the last result and _error the last error; assigning either stops the update, with Expression assignment to _ now disabled.
  • \editor enters editor mode: Enter inserts newlines, ^D submits, ^C cancels
  • \break drops the buffered lines of an unfinished piped input

Piped mode

When stdout is not a tty the REPL runs in line mode, so a script or an LLM can drive it over a pipe (REPL.piped is true):

  • one command per input line, read until EOF, then the REPL finishes; an input with an open bracket continues on the next line
  • no escape sequences, no raw tty mode, no syntax highlighting, and inspect() output has colors: false
  • the prompt is printed once per ready state (> ), so a controller reads the result up to the next prompt
  • an await command holds back the queued lines until it settles
printf '1+1\nawait fetch(url)\n' | qjsm repl-script.js

Exports

ExportArgsKindDescription
REPL—classThe REPL engine: prompt, line editing, history, completion, evaluation, output.
REPLServer—classWraps a REPL to serve a session (Node repl-style).
loadModule(moduleName)1async functionDynamically imports a module for use inside the REPL. A leading * assigns its exports onto globalThis, ! calls its default export, = binds the default export as globalThis.<name> (\i =require); with none the namespace is bound as globalThis.<name>.

Export list

Generated by utilities/doc-api.js from the repl dump of utilities/api-surface.sh: 120 members. Edit the text above this block, not the block.

Functions: loadModule()

REPL

class, constructible

  • methods: abort(), acceptLine(), addCleanupHandler(), alert(), backwardChar(), backwardDeleteChar(), backwardKillLine(), backwardKillMismatch(), backwardKillWord(), backwardWord(), beginningOfLine(), bigfloatToString(), bigintToString(), bracketStyles(), cmdReadlineStart(), cmdStart(), colorizeJs(), completion(), continuation(), controlC(), controlD(), controlL(), defineLastValues(), deleteAll(), deleteChar(), deleteCharDir(), downcaseWord(), dupstr(), endOfLine(), escape(), evalAndPrintStart(), exit(), extractDirective(), forwardChar(), forwardSearch(), forwardWord(), getCompletions(), getContextObject(), getContextWord(), getDirective(), handleByte(), handleChar(), handleCmd(), handleCmdEnd(), handleDirective(), handleKey(), handleMouse(), hasDirective(), help(), historyAdd(), historyClear(), historyFile(), historyLoad(), historyNext(), historyPrevious(), historySave(), historySearch(), historySearchBackward(), historySearchForward(), insert(), killLine(), killRegion(), killWord(), lineEnd(), lineMove(), lineStart(), loadSaveOptions(), makeFilename(), modulesLoad(), modulesSave(), mouseTracking(), moveBelow(), numberToString(), paintLine(), pasteChar(), pasteStart(), pipeChar(), pipeDrain(), print(), printEvalError(), printEvalResult(), printFunction(), printPromise(), printResult(), printStatus(), quotedInsert(), readlineHandleCmd(), readlinePrintPrompt(), readlineRemovePrompt(), readlineStart(), render(), reset(), reverseSearch(), run(), saveOptions(), searchCb(), setupMouseTracking(), show(), showCompletions(), sigintHandler(), skipWordBackward(), skipWordForward(), termInit(), termReadHandler(), transposeChars(), transposeWords(), upcaseWord(), update(), yank()
  • properties: directives, [Symbol.toStringTag]

REPLServer

class, constructible

  • methods: clearBufferedCommand(), defineCommand(), displayPrompt(), setupHistory()