11cb0ef41Sopenharmony_ci# REPL
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!-- source_link=lib/repl.js -->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciThe `node:repl` module provides a Read-Eval-Print-Loop (REPL) implementation
101cb0ef41Sopenharmony_cithat is available both as a standalone program or includible in other
111cb0ef41Sopenharmony_ciapplications. It can be accessed using:
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci```js
141cb0ef41Sopenharmony_ciconst repl = require('node:repl');
151cb0ef41Sopenharmony_ci```
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci## Design and features
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciThe `node:repl` module exports the [`repl.REPLServer`][] class. While running,
201cb0ef41Sopenharmony_ciinstances of [`repl.REPLServer`][] will accept individual lines of user input,
211cb0ef41Sopenharmony_cievaluate those according to a user-defined evaluation function, then output the
221cb0ef41Sopenharmony_ciresult. Input and output may be from `stdin` and `stdout`, respectively, or may
231cb0ef41Sopenharmony_cibe connected to any Node.js [stream][].
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciInstances of [`repl.REPLServer`][] support automatic completion of inputs,
261cb0ef41Sopenharmony_cicompletion preview, simplistic Emacs-style line editing, multi-line inputs,
271cb0ef41Sopenharmony_ci[ZSH][]-like reverse-i-search, [ZSH][]-like substring-based history search,
281cb0ef41Sopenharmony_ciANSI-styled output, saving and restoring current REPL session state, error
291cb0ef41Sopenharmony_cirecovery, and customizable evaluation functions. Terminals that do not support
301cb0ef41Sopenharmony_ciANSI styles and Emacs-style line editing automatically fall back to a limited
311cb0ef41Sopenharmony_cifeature set.
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci### Commands and special keys
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciThe following special commands are supported by all REPL instances:
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci* `.break`: When in the process of inputting a multi-line expression, enter
381cb0ef41Sopenharmony_ci  the `.break` command (or press <kbd>Ctrl</kbd>+<kbd>C</kbd>) to abort
391cb0ef41Sopenharmony_ci  further input or processing of that expression.
401cb0ef41Sopenharmony_ci* `.clear`: Resets the REPL `context` to an empty object and clears any
411cb0ef41Sopenharmony_ci  multi-line expression being input.
421cb0ef41Sopenharmony_ci* `.exit`: Close the I/O stream, causing the REPL to exit.
431cb0ef41Sopenharmony_ci* `.help`: Show this list of special commands.
441cb0ef41Sopenharmony_ci* `.save`: Save the current REPL session to a file:
451cb0ef41Sopenharmony_ci  `> .save ./file/to/save.js`
461cb0ef41Sopenharmony_ci* `.load`: Load a file into the current REPL session.
471cb0ef41Sopenharmony_ci  `> .load ./file/to/load.js`
481cb0ef41Sopenharmony_ci* `.editor`: Enter editor mode (<kbd>Ctrl</kbd>+<kbd>D</kbd> to
491cb0ef41Sopenharmony_ci  finish, <kbd>Ctrl</kbd>+<kbd>C</kbd> to cancel).
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci```console
521cb0ef41Sopenharmony_ci> .editor
531cb0ef41Sopenharmony_ci// Entering editor mode (^D to finish, ^C to cancel)
541cb0ef41Sopenharmony_cifunction welcome(name) {
551cb0ef41Sopenharmony_ci  return `Hello ${name}!`;
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciwelcome('Node.js User');
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci// ^D
611cb0ef41Sopenharmony_ci'Hello Node.js User!'
621cb0ef41Sopenharmony_ci>
631cb0ef41Sopenharmony_ci```
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciThe following key combinations in the REPL have these special effects:
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci* <kbd>Ctrl</kbd>+<kbd>C</kbd>: When pressed once, has the same effect as the
681cb0ef41Sopenharmony_ci  `.break` command.
691cb0ef41Sopenharmony_ci  When pressed twice on a blank line, has the same effect as the `.exit`
701cb0ef41Sopenharmony_ci  command.
711cb0ef41Sopenharmony_ci* <kbd>Ctrl</kbd>+<kbd>D</kbd>: Has the same effect as the `.exit` command.
721cb0ef41Sopenharmony_ci* <kbd>Tab</kbd>: When pressed on a blank line, displays global and local
731cb0ef41Sopenharmony_ci  (scope) variables. When pressed while entering other input, displays relevant
741cb0ef41Sopenharmony_ci  autocompletion options.
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ciFor key bindings related to the reverse-i-search, see [`reverse-i-search`][].
771cb0ef41Sopenharmony_ciFor all other key bindings, see [TTY keybindings][].
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci### Default evaluation
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ciBy default, all instances of [`repl.REPLServer`][] use an evaluation function
821cb0ef41Sopenharmony_cithat evaluates JavaScript expressions and provides access to Node.js built-in
831cb0ef41Sopenharmony_cimodules. This default behavior can be overridden by passing in an alternative
841cb0ef41Sopenharmony_cievaluation function when the [`repl.REPLServer`][] instance is created.
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci#### JavaScript expressions
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ciThe default evaluator supports direct evaluation of JavaScript expressions:
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci```console
911cb0ef41Sopenharmony_ci> 1 + 1
921cb0ef41Sopenharmony_ci2
931cb0ef41Sopenharmony_ci> const m = 2
941cb0ef41Sopenharmony_ciundefined
951cb0ef41Sopenharmony_ci> m + 1
961cb0ef41Sopenharmony_ci3
971cb0ef41Sopenharmony_ci```
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ciUnless otherwise scoped within blocks or functions, variables declared
1001cb0ef41Sopenharmony_cieither implicitly or using the `const`, `let`, or `var` keywords
1011cb0ef41Sopenharmony_ciare declared at the global scope.
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci#### Global and local scope
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ciThe default evaluator provides access to any variables that exist in the global
1061cb0ef41Sopenharmony_ciscope. It is possible to expose a variable to the REPL explicitly by assigning
1071cb0ef41Sopenharmony_ciit to the `context` object associated with each `REPLServer`:
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci```js
1101cb0ef41Sopenharmony_ciconst repl = require('node:repl');
1111cb0ef41Sopenharmony_ciconst msg = 'message';
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_cirepl.start('> ').context.m = msg;
1141cb0ef41Sopenharmony_ci```
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ciProperties in the `context` object appear as local within the REPL:
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci```console
1191cb0ef41Sopenharmony_ci$ node repl_test.js
1201cb0ef41Sopenharmony_ci> m
1211cb0ef41Sopenharmony_ci'message'
1221cb0ef41Sopenharmony_ci```
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ciContext properties are not read-only by default. To specify read-only globals,
1251cb0ef41Sopenharmony_cicontext properties must be defined using `Object.defineProperty()`:
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci```js
1281cb0ef41Sopenharmony_ciconst repl = require('node:repl');
1291cb0ef41Sopenharmony_ciconst msg = 'message';
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ciconst r = repl.start('> ');
1321cb0ef41Sopenharmony_ciObject.defineProperty(r.context, 'm', {
1331cb0ef41Sopenharmony_ci  configurable: false,
1341cb0ef41Sopenharmony_ci  enumerable: true,
1351cb0ef41Sopenharmony_ci  value: msg,
1361cb0ef41Sopenharmony_ci});
1371cb0ef41Sopenharmony_ci```
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci#### Accessing core Node.js modules
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ciThe default evaluator will automatically load Node.js core modules into the
1421cb0ef41Sopenharmony_ciREPL environment when used. For instance, unless otherwise declared as a
1431cb0ef41Sopenharmony_ciglobal or scoped variable, the input `fs` will be evaluated on-demand as
1441cb0ef41Sopenharmony_ci`global.fs = require('node:fs')`.
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci```console
1471cb0ef41Sopenharmony_ci> fs.createReadStream('./some/file');
1481cb0ef41Sopenharmony_ci```
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci#### Global uncaught exceptions
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci<!-- YAML
1531cb0ef41Sopenharmony_cichanges:
1541cb0ef41Sopenharmony_ci  - version: v12.3.0
1551cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/27151
1561cb0ef41Sopenharmony_ci    description: The `'uncaughtException'` event is from now on triggered if the
1571cb0ef41Sopenharmony_ci                 repl is used as standalone program.
1581cb0ef41Sopenharmony_ci-->
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ciThe REPL uses the [`domain`][] module to catch all uncaught exceptions for that
1611cb0ef41Sopenharmony_ciREPL session.
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ciThis use of the [`domain`][] module in the REPL has these side effects:
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci* Uncaught exceptions only emit the [`'uncaughtException'`][] event in the
1661cb0ef41Sopenharmony_ci  standalone REPL. Adding a listener for this event in a REPL within
1671cb0ef41Sopenharmony_ci  another Node.js program results in [`ERR_INVALID_REPL_INPUT`][].
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci  ```js
1701cb0ef41Sopenharmony_ci  const r = repl.start();
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci  r.write('process.on("uncaughtException", () => console.log("Foobar"));\n');
1731cb0ef41Sopenharmony_ci  // Output stream includes:
1741cb0ef41Sopenharmony_ci  //   TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtException`
1751cb0ef41Sopenharmony_ci  //   cannot be used in the REPL
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci  r.close();
1781cb0ef41Sopenharmony_ci  ```
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci* Trying to use [`process.setUncaughtExceptionCaptureCallback()`][] throws
1811cb0ef41Sopenharmony_ci  an [`ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE`][] error.
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci#### Assignment of the `_` (underscore) variable
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ci<!-- YAML
1861cb0ef41Sopenharmony_cichanges:
1871cb0ef41Sopenharmony_ci  - version: v9.8.0
1881cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/18919
1891cb0ef41Sopenharmony_ci    description: Added `_error` support.
1901cb0ef41Sopenharmony_ci-->
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ciThe default evaluator will, by default, assign the result of the most recently
1931cb0ef41Sopenharmony_cievaluated expression to the special variable `_` (underscore).
1941cb0ef41Sopenharmony_ciExplicitly setting `_` to a value will disable this behavior.
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci```console
1971cb0ef41Sopenharmony_ci> [ 'a', 'b', 'c' ]
1981cb0ef41Sopenharmony_ci[ 'a', 'b', 'c' ]
1991cb0ef41Sopenharmony_ci> _.length
2001cb0ef41Sopenharmony_ci3
2011cb0ef41Sopenharmony_ci> _ += 1
2021cb0ef41Sopenharmony_ciExpression assignment to _ now disabled.
2031cb0ef41Sopenharmony_ci4
2041cb0ef41Sopenharmony_ci> 1 + 1
2051cb0ef41Sopenharmony_ci2
2061cb0ef41Sopenharmony_ci> _
2071cb0ef41Sopenharmony_ci4
2081cb0ef41Sopenharmony_ci```
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ciSimilarly, `_error` will refer to the last seen error, if there was any.
2111cb0ef41Sopenharmony_ciExplicitly setting `_error` to a value will disable this behavior.
2121cb0ef41Sopenharmony_ci
2131cb0ef41Sopenharmony_ci```console
2141cb0ef41Sopenharmony_ci> throw new Error('foo');
2151cb0ef41Sopenharmony_ciUncaught Error: foo
2161cb0ef41Sopenharmony_ci> _error.message
2171cb0ef41Sopenharmony_ci'foo'
2181cb0ef41Sopenharmony_ci```
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci#### `await` keyword
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ciSupport for the `await` keyword is enabled at the top level.
2231cb0ef41Sopenharmony_ci
2241cb0ef41Sopenharmony_ci```console
2251cb0ef41Sopenharmony_ci> await Promise.resolve(123)
2261cb0ef41Sopenharmony_ci123
2271cb0ef41Sopenharmony_ci> await Promise.reject(new Error('REPL await'))
2281cb0ef41Sopenharmony_ciUncaught Error: REPL await
2291cb0ef41Sopenharmony_ci    at REPL2:1:54
2301cb0ef41Sopenharmony_ci> const timeout = util.promisify(setTimeout);
2311cb0ef41Sopenharmony_ciundefined
2321cb0ef41Sopenharmony_ci> const old = Date.now(); await timeout(1000); console.log(Date.now() - old);
2331cb0ef41Sopenharmony_ci1002
2341cb0ef41Sopenharmony_ciundefined
2351cb0ef41Sopenharmony_ci```
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ciOne known limitation of using the `await` keyword in the REPL is that
2381cb0ef41Sopenharmony_ciit will invalidate the lexical scoping of the `const` and `let`
2391cb0ef41Sopenharmony_cikeywords.
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ciFor example:
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_ci```console
2441cb0ef41Sopenharmony_ci> const m = await Promise.resolve(123)
2451cb0ef41Sopenharmony_ciundefined
2461cb0ef41Sopenharmony_ci> m
2471cb0ef41Sopenharmony_ci123
2481cb0ef41Sopenharmony_ci> const m = await Promise.resolve(234)
2491cb0ef41Sopenharmony_ciundefined
2501cb0ef41Sopenharmony_ci> m
2511cb0ef41Sopenharmony_ci234
2521cb0ef41Sopenharmony_ci```
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci[`--no-experimental-repl-await`][] shall disable top-level await in REPL.
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ci### Reverse-i-search
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_ci<!-- YAML
2591cb0ef41Sopenharmony_ciadded:
2601cb0ef41Sopenharmony_ci - v13.6.0
2611cb0ef41Sopenharmony_ci - v12.17.0
2621cb0ef41Sopenharmony_ci-->
2631cb0ef41Sopenharmony_ci
2641cb0ef41Sopenharmony_ciThe REPL supports bi-directional reverse-i-search similar to [ZSH][]. It is
2651cb0ef41Sopenharmony_citriggered with <kbd>Ctrl</kbd>+<kbd>R</kbd> to search backward
2661cb0ef41Sopenharmony_ciand <kbd>Ctrl</kbd>+<kbd>S</kbd> to search forwards.
2671cb0ef41Sopenharmony_ci
2681cb0ef41Sopenharmony_ciDuplicated history entries will be skipped.
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ciEntries are accepted as soon as any key is pressed that doesn't correspond
2711cb0ef41Sopenharmony_ciwith the reverse search. Cancelling is possible by pressing <kbd>Esc</kbd>
2721cb0ef41Sopenharmony_cior <kbd>Ctrl</kbd>+<kbd>C</kbd>.
2731cb0ef41Sopenharmony_ci
2741cb0ef41Sopenharmony_ciChanging the direction immediately searches for the next entry in the expected
2751cb0ef41Sopenharmony_cidirection from the current position on.
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_ci### Custom evaluation functions
2781cb0ef41Sopenharmony_ci
2791cb0ef41Sopenharmony_ciWhen a new [`repl.REPLServer`][] is created, a custom evaluation function may be
2801cb0ef41Sopenharmony_ciprovided. This can be used, for instance, to implement fully customized REPL
2811cb0ef41Sopenharmony_ciapplications.
2821cb0ef41Sopenharmony_ci
2831cb0ef41Sopenharmony_ciThe following illustrates a hypothetical example of a REPL that performs
2841cb0ef41Sopenharmony_citranslation of text from one language to another:
2851cb0ef41Sopenharmony_ci
2861cb0ef41Sopenharmony_ci```js
2871cb0ef41Sopenharmony_ciconst repl = require('node:repl');
2881cb0ef41Sopenharmony_ciconst { Translator } = require('translator');
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ciconst myTranslator = new Translator('en', 'fr');
2911cb0ef41Sopenharmony_ci
2921cb0ef41Sopenharmony_cifunction myEval(cmd, context, filename, callback) {
2931cb0ef41Sopenharmony_ci  callback(null, myTranslator.translate(cmd));
2941cb0ef41Sopenharmony_ci}
2951cb0ef41Sopenharmony_ci
2961cb0ef41Sopenharmony_cirepl.start({ prompt: '> ', eval: myEval });
2971cb0ef41Sopenharmony_ci```
2981cb0ef41Sopenharmony_ci
2991cb0ef41Sopenharmony_ci#### Recoverable errors
3001cb0ef41Sopenharmony_ci
3011cb0ef41Sopenharmony_ciAt the REPL prompt, pressing <kbd>Enter</kbd> sends the current line of input to
3021cb0ef41Sopenharmony_cithe `eval` function. In order to support multi-line input, the `eval` function
3031cb0ef41Sopenharmony_cican return an instance of `repl.Recoverable` to the provided callback function:
3041cb0ef41Sopenharmony_ci
3051cb0ef41Sopenharmony_ci```js
3061cb0ef41Sopenharmony_cifunction myEval(cmd, context, filename, callback) {
3071cb0ef41Sopenharmony_ci  let result;
3081cb0ef41Sopenharmony_ci  try {
3091cb0ef41Sopenharmony_ci    result = vm.runInThisContext(cmd);
3101cb0ef41Sopenharmony_ci  } catch (e) {
3111cb0ef41Sopenharmony_ci    if (isRecoverableError(e)) {
3121cb0ef41Sopenharmony_ci      return callback(new repl.Recoverable(e));
3131cb0ef41Sopenharmony_ci    }
3141cb0ef41Sopenharmony_ci  }
3151cb0ef41Sopenharmony_ci  callback(null, result);
3161cb0ef41Sopenharmony_ci}
3171cb0ef41Sopenharmony_ci
3181cb0ef41Sopenharmony_cifunction isRecoverableError(error) {
3191cb0ef41Sopenharmony_ci  if (error.name === 'SyntaxError') {
3201cb0ef41Sopenharmony_ci    return /^(Unexpected end of input|Unexpected token)/.test(error.message);
3211cb0ef41Sopenharmony_ci  }
3221cb0ef41Sopenharmony_ci  return false;
3231cb0ef41Sopenharmony_ci}
3241cb0ef41Sopenharmony_ci```
3251cb0ef41Sopenharmony_ci
3261cb0ef41Sopenharmony_ci### Customizing REPL output
3271cb0ef41Sopenharmony_ci
3281cb0ef41Sopenharmony_ciBy default, [`repl.REPLServer`][] instances format output using the
3291cb0ef41Sopenharmony_ci[`util.inspect()`][] method before writing the output to the provided `Writable`
3301cb0ef41Sopenharmony_cistream (`process.stdout` by default). The `showProxy` inspection option is set
3311cb0ef41Sopenharmony_cito true by default and the `colors` option is set to true depending on the
3321cb0ef41Sopenharmony_ciREPL's `useColors` option.
3331cb0ef41Sopenharmony_ci
3341cb0ef41Sopenharmony_ciThe `useColors` boolean option can be specified at construction to instruct the
3351cb0ef41Sopenharmony_cidefault writer to use ANSI style codes to colorize the output from the
3361cb0ef41Sopenharmony_ci`util.inspect()` method.
3371cb0ef41Sopenharmony_ci
3381cb0ef41Sopenharmony_ciIf the REPL is run as standalone program, it is also possible to change the
3391cb0ef41Sopenharmony_ciREPL's [inspection defaults][`util.inspect()`] from inside the REPL by using the
3401cb0ef41Sopenharmony_ci`inspect.replDefaults` property which mirrors the `defaultOptions` from
3411cb0ef41Sopenharmony_ci[`util.inspect()`][].
3421cb0ef41Sopenharmony_ci
3431cb0ef41Sopenharmony_ci```console
3441cb0ef41Sopenharmony_ci> util.inspect.replDefaults.compact = false;
3451cb0ef41Sopenharmony_cifalse
3461cb0ef41Sopenharmony_ci> [1]
3471cb0ef41Sopenharmony_ci[
3481cb0ef41Sopenharmony_ci  1
3491cb0ef41Sopenharmony_ci]
3501cb0ef41Sopenharmony_ci>
3511cb0ef41Sopenharmony_ci```
3521cb0ef41Sopenharmony_ci
3531cb0ef41Sopenharmony_ciTo fully customize the output of a [`repl.REPLServer`][] instance pass in a new
3541cb0ef41Sopenharmony_cifunction for the `writer` option on construction. The following example, for
3551cb0ef41Sopenharmony_ciinstance, simply converts any input text to upper case:
3561cb0ef41Sopenharmony_ci
3571cb0ef41Sopenharmony_ci```js
3581cb0ef41Sopenharmony_ciconst repl = require('node:repl');
3591cb0ef41Sopenharmony_ci
3601cb0ef41Sopenharmony_ciconst r = repl.start({ prompt: '> ', eval: myEval, writer: myWriter });
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_cifunction myEval(cmd, context, filename, callback) {
3631cb0ef41Sopenharmony_ci  callback(null, cmd);
3641cb0ef41Sopenharmony_ci}
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_cifunction myWriter(output) {
3671cb0ef41Sopenharmony_ci  return output.toUpperCase();
3681cb0ef41Sopenharmony_ci}
3691cb0ef41Sopenharmony_ci```
3701cb0ef41Sopenharmony_ci
3711cb0ef41Sopenharmony_ci## Class: `REPLServer`
3721cb0ef41Sopenharmony_ci
3731cb0ef41Sopenharmony_ci<!-- YAML
3741cb0ef41Sopenharmony_ciadded: v0.1.91
3751cb0ef41Sopenharmony_ci-->
3761cb0ef41Sopenharmony_ci
3771cb0ef41Sopenharmony_ci* `options` {Object|string} See [`repl.start()`][]
3781cb0ef41Sopenharmony_ci* Extends: {readline.Interface}
3791cb0ef41Sopenharmony_ci
3801cb0ef41Sopenharmony_ciInstances of `repl.REPLServer` are created using the [`repl.start()`][] method
3811cb0ef41Sopenharmony_cior directly using the JavaScript `new` keyword.
3821cb0ef41Sopenharmony_ci
3831cb0ef41Sopenharmony_ci```js
3841cb0ef41Sopenharmony_ciconst repl = require('node:repl');
3851cb0ef41Sopenharmony_ci
3861cb0ef41Sopenharmony_ciconst options = { useColors: true };
3871cb0ef41Sopenharmony_ci
3881cb0ef41Sopenharmony_ciconst firstInstance = repl.start(options);
3891cb0ef41Sopenharmony_ciconst secondInstance = new repl.REPLServer(options);
3901cb0ef41Sopenharmony_ci```
3911cb0ef41Sopenharmony_ci
3921cb0ef41Sopenharmony_ci### Event: `'exit'`
3931cb0ef41Sopenharmony_ci
3941cb0ef41Sopenharmony_ci<!-- YAML
3951cb0ef41Sopenharmony_ciadded: v0.7.7
3961cb0ef41Sopenharmony_ci-->
3971cb0ef41Sopenharmony_ci
3981cb0ef41Sopenharmony_ciThe `'exit'` event is emitted when the REPL is exited either by receiving the
3991cb0ef41Sopenharmony_ci`.exit` command as input, the user pressing <kbd>Ctrl</kbd>+<kbd>C</kbd> twice
4001cb0ef41Sopenharmony_cito signal `SIGINT`,
4011cb0ef41Sopenharmony_cior by pressing <kbd>Ctrl</kbd>+<kbd>D</kbd> to signal `'end'` on the input
4021cb0ef41Sopenharmony_cistream. The listener
4031cb0ef41Sopenharmony_cicallback is invoked without any arguments.
4041cb0ef41Sopenharmony_ci
4051cb0ef41Sopenharmony_ci```js
4061cb0ef41Sopenharmony_cireplServer.on('exit', () => {
4071cb0ef41Sopenharmony_ci  console.log('Received "exit" event from repl!');
4081cb0ef41Sopenharmony_ci  process.exit();
4091cb0ef41Sopenharmony_ci});
4101cb0ef41Sopenharmony_ci```
4111cb0ef41Sopenharmony_ci
4121cb0ef41Sopenharmony_ci### Event: `'reset'`
4131cb0ef41Sopenharmony_ci
4141cb0ef41Sopenharmony_ci<!-- YAML
4151cb0ef41Sopenharmony_ciadded: v0.11.0
4161cb0ef41Sopenharmony_ci-->
4171cb0ef41Sopenharmony_ci
4181cb0ef41Sopenharmony_ciThe `'reset'` event is emitted when the REPL's context is reset. This occurs
4191cb0ef41Sopenharmony_ciwhenever the `.clear` command is received as input _unless_ the REPL is using
4201cb0ef41Sopenharmony_cithe default evaluator and the `repl.REPLServer` instance was created with the
4211cb0ef41Sopenharmony_ci`useGlobal` option set to `true`. The listener callback will be called with a
4221cb0ef41Sopenharmony_cireference to the `context` object as the only argument.
4231cb0ef41Sopenharmony_ci
4241cb0ef41Sopenharmony_ciThis can be used primarily to re-initialize REPL context to some pre-defined
4251cb0ef41Sopenharmony_cistate:
4261cb0ef41Sopenharmony_ci
4271cb0ef41Sopenharmony_ci```js
4281cb0ef41Sopenharmony_ciconst repl = require('node:repl');
4291cb0ef41Sopenharmony_ci
4301cb0ef41Sopenharmony_cifunction initializeContext(context) {
4311cb0ef41Sopenharmony_ci  context.m = 'test';
4321cb0ef41Sopenharmony_ci}
4331cb0ef41Sopenharmony_ci
4341cb0ef41Sopenharmony_ciconst r = repl.start({ prompt: '> ' });
4351cb0ef41Sopenharmony_ciinitializeContext(r.context);
4361cb0ef41Sopenharmony_ci
4371cb0ef41Sopenharmony_cir.on('reset', initializeContext);
4381cb0ef41Sopenharmony_ci```
4391cb0ef41Sopenharmony_ci
4401cb0ef41Sopenharmony_ciWhen this code is executed, the global `'m'` variable can be modified but then
4411cb0ef41Sopenharmony_cireset to its initial value using the `.clear` command:
4421cb0ef41Sopenharmony_ci
4431cb0ef41Sopenharmony_ci```console
4441cb0ef41Sopenharmony_ci$ ./node example.js
4451cb0ef41Sopenharmony_ci> m
4461cb0ef41Sopenharmony_ci'test'
4471cb0ef41Sopenharmony_ci> m = 1
4481cb0ef41Sopenharmony_ci1
4491cb0ef41Sopenharmony_ci> m
4501cb0ef41Sopenharmony_ci1
4511cb0ef41Sopenharmony_ci> .clear
4521cb0ef41Sopenharmony_ciClearing context...
4531cb0ef41Sopenharmony_ci> m
4541cb0ef41Sopenharmony_ci'test'
4551cb0ef41Sopenharmony_ci>
4561cb0ef41Sopenharmony_ci```
4571cb0ef41Sopenharmony_ci
4581cb0ef41Sopenharmony_ci### `replServer.defineCommand(keyword, cmd)`
4591cb0ef41Sopenharmony_ci
4601cb0ef41Sopenharmony_ci<!-- YAML
4611cb0ef41Sopenharmony_ciadded: v0.3.0
4621cb0ef41Sopenharmony_ci-->
4631cb0ef41Sopenharmony_ci
4641cb0ef41Sopenharmony_ci* `keyword` {string} The command keyword (_without_ a leading `.` character).
4651cb0ef41Sopenharmony_ci* `cmd` {Object|Function} The function to invoke when the command is processed.
4661cb0ef41Sopenharmony_ci
4671cb0ef41Sopenharmony_ciThe `replServer.defineCommand()` method is used to add new `.`-prefixed commands
4681cb0ef41Sopenharmony_cito the REPL instance. Such commands are invoked by typing a `.` followed by the
4691cb0ef41Sopenharmony_ci`keyword`. The `cmd` is either a `Function` or an `Object` with the following
4701cb0ef41Sopenharmony_ciproperties:
4711cb0ef41Sopenharmony_ci
4721cb0ef41Sopenharmony_ci* `help` {string} Help text to be displayed when `.help` is entered (Optional).
4731cb0ef41Sopenharmony_ci* `action` {Function} The function to execute, optionally accepting a single
4741cb0ef41Sopenharmony_ci  string argument.
4751cb0ef41Sopenharmony_ci
4761cb0ef41Sopenharmony_ciThe following example shows two new commands added to the REPL instance:
4771cb0ef41Sopenharmony_ci
4781cb0ef41Sopenharmony_ci```js
4791cb0ef41Sopenharmony_ciconst repl = require('node:repl');
4801cb0ef41Sopenharmony_ci
4811cb0ef41Sopenharmony_ciconst replServer = repl.start({ prompt: '> ' });
4821cb0ef41Sopenharmony_cireplServer.defineCommand('sayhello', {
4831cb0ef41Sopenharmony_ci  help: 'Say hello',
4841cb0ef41Sopenharmony_ci  action(name) {
4851cb0ef41Sopenharmony_ci    this.clearBufferedCommand();
4861cb0ef41Sopenharmony_ci    console.log(`Hello, ${name}!`);
4871cb0ef41Sopenharmony_ci    this.displayPrompt();
4881cb0ef41Sopenharmony_ci  },
4891cb0ef41Sopenharmony_ci});
4901cb0ef41Sopenharmony_cireplServer.defineCommand('saybye', function saybye() {
4911cb0ef41Sopenharmony_ci  console.log('Goodbye!');
4921cb0ef41Sopenharmony_ci  this.close();
4931cb0ef41Sopenharmony_ci});
4941cb0ef41Sopenharmony_ci```
4951cb0ef41Sopenharmony_ci
4961cb0ef41Sopenharmony_ciThe new commands can then be used from within the REPL instance:
4971cb0ef41Sopenharmony_ci
4981cb0ef41Sopenharmony_ci```console
4991cb0ef41Sopenharmony_ci> .sayhello Node.js User
5001cb0ef41Sopenharmony_ciHello, Node.js User!
5011cb0ef41Sopenharmony_ci> .saybye
5021cb0ef41Sopenharmony_ciGoodbye!
5031cb0ef41Sopenharmony_ci```
5041cb0ef41Sopenharmony_ci
5051cb0ef41Sopenharmony_ci### `replServer.displayPrompt([preserveCursor])`
5061cb0ef41Sopenharmony_ci
5071cb0ef41Sopenharmony_ci<!-- YAML
5081cb0ef41Sopenharmony_ciadded: v0.1.91
5091cb0ef41Sopenharmony_ci-->
5101cb0ef41Sopenharmony_ci
5111cb0ef41Sopenharmony_ci* `preserveCursor` {boolean}
5121cb0ef41Sopenharmony_ci
5131cb0ef41Sopenharmony_ciThe `replServer.displayPrompt()` method readies the REPL instance for input
5141cb0ef41Sopenharmony_cifrom the user, printing the configured `prompt` to a new line in the `output`
5151cb0ef41Sopenharmony_ciand resuming the `input` to accept new input.
5161cb0ef41Sopenharmony_ci
5171cb0ef41Sopenharmony_ciWhen multi-line input is being entered, an ellipsis is printed rather than the
5181cb0ef41Sopenharmony_ci'prompt'.
5191cb0ef41Sopenharmony_ci
5201cb0ef41Sopenharmony_ciWhen `preserveCursor` is `true`, the cursor placement will not be reset to `0`.
5211cb0ef41Sopenharmony_ci
5221cb0ef41Sopenharmony_ciThe `replServer.displayPrompt` method is primarily intended to be called from
5231cb0ef41Sopenharmony_ciwithin the action function for commands registered using the
5241cb0ef41Sopenharmony_ci`replServer.defineCommand()` method.
5251cb0ef41Sopenharmony_ci
5261cb0ef41Sopenharmony_ci### `replServer.clearBufferedCommand()`
5271cb0ef41Sopenharmony_ci
5281cb0ef41Sopenharmony_ci<!-- YAML
5291cb0ef41Sopenharmony_ciadded: v9.0.0
5301cb0ef41Sopenharmony_ci-->
5311cb0ef41Sopenharmony_ci
5321cb0ef41Sopenharmony_ciThe `replServer.clearBufferedCommand()` method clears any command that has been
5331cb0ef41Sopenharmony_cibuffered but not yet executed. This method is primarily intended to be
5341cb0ef41Sopenharmony_cicalled from within the action function for commands registered using the
5351cb0ef41Sopenharmony_ci`replServer.defineCommand()` method.
5361cb0ef41Sopenharmony_ci
5371cb0ef41Sopenharmony_ci### `replServer.parseREPLKeyword(keyword[, rest])`
5381cb0ef41Sopenharmony_ci
5391cb0ef41Sopenharmony_ci<!-- YAML
5401cb0ef41Sopenharmony_ciadded: v0.8.9
5411cb0ef41Sopenharmony_cideprecated: v9.0.0
5421cb0ef41Sopenharmony_ci-->
5431cb0ef41Sopenharmony_ci
5441cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated.
5451cb0ef41Sopenharmony_ci
5461cb0ef41Sopenharmony_ci* `keyword` {string} the potential keyword to parse and execute
5471cb0ef41Sopenharmony_ci* `rest` {any} any parameters to the keyword command
5481cb0ef41Sopenharmony_ci* Returns: {boolean}
5491cb0ef41Sopenharmony_ci
5501cb0ef41Sopenharmony_ciAn internal method used to parse and execute `REPLServer` keywords.
5511cb0ef41Sopenharmony_ciReturns `true` if `keyword` is a valid keyword, otherwise `false`.
5521cb0ef41Sopenharmony_ci
5531cb0ef41Sopenharmony_ci### `replServer.setupHistory(historyPath, callback)`
5541cb0ef41Sopenharmony_ci
5551cb0ef41Sopenharmony_ci<!-- YAML
5561cb0ef41Sopenharmony_ciadded: v11.10.0
5571cb0ef41Sopenharmony_ci-->
5581cb0ef41Sopenharmony_ci
5591cb0ef41Sopenharmony_ci* `historyPath` {string} the path to the history file
5601cb0ef41Sopenharmony_ci* `callback` {Function} called when history writes are ready or upon error
5611cb0ef41Sopenharmony_ci  * `err` {Error}
5621cb0ef41Sopenharmony_ci  * `repl` {repl.REPLServer}
5631cb0ef41Sopenharmony_ci
5641cb0ef41Sopenharmony_ciInitializes a history log file for the REPL instance. When executing the
5651cb0ef41Sopenharmony_ciNode.js binary and using the command-line REPL, a history file is initialized
5661cb0ef41Sopenharmony_ciby default. However, this is not the case when creating a REPL
5671cb0ef41Sopenharmony_ciprogrammatically. Use this method to initialize a history log file when working
5681cb0ef41Sopenharmony_ciwith REPL instances programmatically.
5691cb0ef41Sopenharmony_ci
5701cb0ef41Sopenharmony_ci## `repl.builtinModules`
5711cb0ef41Sopenharmony_ci
5721cb0ef41Sopenharmony_ci<!-- YAML
5731cb0ef41Sopenharmony_ciadded: v14.5.0
5741cb0ef41Sopenharmony_ci-->
5751cb0ef41Sopenharmony_ci
5761cb0ef41Sopenharmony_ci* {string\[]}
5771cb0ef41Sopenharmony_ci
5781cb0ef41Sopenharmony_ciA list of the names of all Node.js modules, e.g., `'http'`.
5791cb0ef41Sopenharmony_ci
5801cb0ef41Sopenharmony_ci## `repl.start([options])`
5811cb0ef41Sopenharmony_ci
5821cb0ef41Sopenharmony_ci<!-- YAML
5831cb0ef41Sopenharmony_ciadded: v0.1.91
5841cb0ef41Sopenharmony_cichanges:
5851cb0ef41Sopenharmony_ci  - version:
5861cb0ef41Sopenharmony_ci     - v13.4.0
5871cb0ef41Sopenharmony_ci     - v12.17.0
5881cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/30811
5891cb0ef41Sopenharmony_ci    description: The `preview` option is now available.
5901cb0ef41Sopenharmony_ci  - version: v12.0.0
5911cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/26518
5921cb0ef41Sopenharmony_ci    description: The `terminal` option now follows the default description in
5931cb0ef41Sopenharmony_ci                 all cases and `useColors` checks `hasColors()` if available.
5941cb0ef41Sopenharmony_ci  - version: v10.0.0
5951cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/19187
5961cb0ef41Sopenharmony_ci    description: The `REPL_MAGIC_MODE` `replMode` was removed.
5971cb0ef41Sopenharmony_ci  - version: v6.3.0
5981cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/6635
5991cb0ef41Sopenharmony_ci    description: The `breakEvalOnSigint` option is supported now.
6001cb0ef41Sopenharmony_ci  - version: v5.8.0
6011cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/5388
6021cb0ef41Sopenharmony_ci    description: The `options` parameter is optional now.
6031cb0ef41Sopenharmony_ci-->
6041cb0ef41Sopenharmony_ci
6051cb0ef41Sopenharmony_ci* `options` {Object|string}
6061cb0ef41Sopenharmony_ci  * `prompt` {string} The input prompt to display. **Default:** `'> '`
6071cb0ef41Sopenharmony_ci    (with a trailing space).
6081cb0ef41Sopenharmony_ci  * `input` {stream.Readable} The `Readable` stream from which REPL input will
6091cb0ef41Sopenharmony_ci    be read. **Default:** `process.stdin`.
6101cb0ef41Sopenharmony_ci  * `output` {stream.Writable} The `Writable` stream to which REPL output will
6111cb0ef41Sopenharmony_ci    be written. **Default:** `process.stdout`.
6121cb0ef41Sopenharmony_ci  * `terminal` {boolean} If `true`, specifies that the `output` should be
6131cb0ef41Sopenharmony_ci    treated as a TTY terminal.
6141cb0ef41Sopenharmony_ci    **Default:** checking the value of the `isTTY` property on the `output`
6151cb0ef41Sopenharmony_ci    stream upon instantiation.
6161cb0ef41Sopenharmony_ci  * `eval` {Function} The function to be used when evaluating each given line
6171cb0ef41Sopenharmony_ci    of input. **Default:** an async wrapper for the JavaScript `eval()`
6181cb0ef41Sopenharmony_ci    function. An `eval` function can error with `repl.Recoverable` to indicate
6191cb0ef41Sopenharmony_ci    the input was incomplete and prompt for additional lines.
6201cb0ef41Sopenharmony_ci  * `useColors` {boolean} If `true`, specifies that the default `writer`
6211cb0ef41Sopenharmony_ci    function should include ANSI color styling to REPL output. If a custom
6221cb0ef41Sopenharmony_ci    `writer` function is provided then this has no effect. **Default:** checking
6231cb0ef41Sopenharmony_ci    color support on the `output` stream if the REPL instance's `terminal` value
6241cb0ef41Sopenharmony_ci    is `true`.
6251cb0ef41Sopenharmony_ci  * `useGlobal` {boolean} If `true`, specifies that the default evaluation
6261cb0ef41Sopenharmony_ci    function will use the JavaScript `global` as the context as opposed to
6271cb0ef41Sopenharmony_ci    creating a new separate context for the REPL instance. The node CLI REPL
6281cb0ef41Sopenharmony_ci    sets this value to `true`. **Default:** `false`.
6291cb0ef41Sopenharmony_ci  * `ignoreUndefined` {boolean} If `true`, specifies that the default writer
6301cb0ef41Sopenharmony_ci    will not output the return value of a command if it evaluates to
6311cb0ef41Sopenharmony_ci    `undefined`. **Default:** `false`.
6321cb0ef41Sopenharmony_ci  * `writer` {Function} The function to invoke to format the output of each
6331cb0ef41Sopenharmony_ci    command before writing to `output`. **Default:** [`util.inspect()`][].
6341cb0ef41Sopenharmony_ci  * `completer` {Function} An optional function used for custom Tab auto
6351cb0ef41Sopenharmony_ci    completion. See [`readline.InterfaceCompleter`][] for an example.
6361cb0ef41Sopenharmony_ci  * `replMode` {symbol} A flag that specifies whether the default evaluator
6371cb0ef41Sopenharmony_ci    executes all JavaScript commands in strict mode or default (sloppy) mode.
6381cb0ef41Sopenharmony_ci    Acceptable values are:
6391cb0ef41Sopenharmony_ci    * `repl.REPL_MODE_SLOPPY` to evaluate expressions in sloppy mode.
6401cb0ef41Sopenharmony_ci    * `repl.REPL_MODE_STRICT` to evaluate expressions in strict mode. This is
6411cb0ef41Sopenharmony_ci      equivalent to prefacing every repl statement with `'use strict'`.
6421cb0ef41Sopenharmony_ci  * `breakEvalOnSigint` {boolean} Stop evaluating the current piece of code when
6431cb0ef41Sopenharmony_ci    `SIGINT` is received, such as when <kbd>Ctrl</kbd>+<kbd>C</kbd> is pressed.
6441cb0ef41Sopenharmony_ci    This cannot be used
6451cb0ef41Sopenharmony_ci    together with a custom `eval` function. **Default:** `false`.
6461cb0ef41Sopenharmony_ci  * `preview` {boolean} Defines if the repl prints autocomplete and output
6471cb0ef41Sopenharmony_ci    previews or not. **Default:** `true` with the default eval function and
6481cb0ef41Sopenharmony_ci    `false` in case a custom eval function is used. If `terminal` is falsy, then
6491cb0ef41Sopenharmony_ci    there are no previews and the value of `preview` has no effect.
6501cb0ef41Sopenharmony_ci* Returns: {repl.REPLServer}
6511cb0ef41Sopenharmony_ci
6521cb0ef41Sopenharmony_ciThe `repl.start()` method creates and starts a [`repl.REPLServer`][] instance.
6531cb0ef41Sopenharmony_ci
6541cb0ef41Sopenharmony_ciIf `options` is a string, then it specifies the input prompt:
6551cb0ef41Sopenharmony_ci
6561cb0ef41Sopenharmony_ci```js
6571cb0ef41Sopenharmony_ciconst repl = require('node:repl');
6581cb0ef41Sopenharmony_ci
6591cb0ef41Sopenharmony_ci// a Unix style prompt
6601cb0ef41Sopenharmony_cirepl.start('$ ');
6611cb0ef41Sopenharmony_ci```
6621cb0ef41Sopenharmony_ci
6631cb0ef41Sopenharmony_ci## The Node.js REPL
6641cb0ef41Sopenharmony_ci
6651cb0ef41Sopenharmony_ciNode.js itself uses the `node:repl` module to provide its own interactive
6661cb0ef41Sopenharmony_ciinterface for executing JavaScript. This can be used by executing the Node.js
6671cb0ef41Sopenharmony_cibinary without passing any arguments (or by passing the `-i` argument):
6681cb0ef41Sopenharmony_ci
6691cb0ef41Sopenharmony_ci```console
6701cb0ef41Sopenharmony_ci$ node
6711cb0ef41Sopenharmony_ci> const a = [1, 2, 3];
6721cb0ef41Sopenharmony_ciundefined
6731cb0ef41Sopenharmony_ci> a
6741cb0ef41Sopenharmony_ci[ 1, 2, 3 ]
6751cb0ef41Sopenharmony_ci> a.forEach((v) => {
6761cb0ef41Sopenharmony_ci...   console.log(v);
6771cb0ef41Sopenharmony_ci...   });
6781cb0ef41Sopenharmony_ci1
6791cb0ef41Sopenharmony_ci2
6801cb0ef41Sopenharmony_ci3
6811cb0ef41Sopenharmony_ci```
6821cb0ef41Sopenharmony_ci
6831cb0ef41Sopenharmony_ci### Environment variable options
6841cb0ef41Sopenharmony_ci
6851cb0ef41Sopenharmony_ciVarious behaviors of the Node.js REPL can be customized using the following
6861cb0ef41Sopenharmony_cienvironment variables:
6871cb0ef41Sopenharmony_ci
6881cb0ef41Sopenharmony_ci* `NODE_REPL_HISTORY`: When a valid path is given, persistent REPL history
6891cb0ef41Sopenharmony_ci  will be saved to the specified file rather than `.node_repl_history` in the
6901cb0ef41Sopenharmony_ci  user's home directory. Setting this value to `''` (an empty string) will
6911cb0ef41Sopenharmony_ci  disable persistent REPL history. Whitespace will be trimmed from the value.
6921cb0ef41Sopenharmony_ci  On Windows platforms environment variables with empty values are invalid so
6931cb0ef41Sopenharmony_ci  set this variable to one or more spaces to disable persistent REPL history.
6941cb0ef41Sopenharmony_ci* `NODE_REPL_HISTORY_SIZE`: Controls how many lines of history will be
6951cb0ef41Sopenharmony_ci  persisted if history is available. Must be a positive number.
6961cb0ef41Sopenharmony_ci  **Default:** `1000`.
6971cb0ef41Sopenharmony_ci* `NODE_REPL_MODE`: May be either `'sloppy'` or `'strict'`. **Default:**
6981cb0ef41Sopenharmony_ci  `'sloppy'`, which will allow non-strict mode code to be run.
6991cb0ef41Sopenharmony_ci
7001cb0ef41Sopenharmony_ci### Persistent history
7011cb0ef41Sopenharmony_ci
7021cb0ef41Sopenharmony_ciBy default, the Node.js REPL will persist history between `node` REPL sessions
7031cb0ef41Sopenharmony_ciby saving inputs to a `.node_repl_history` file located in the user's home
7041cb0ef41Sopenharmony_cidirectory. This can be disabled by setting the environment variable
7051cb0ef41Sopenharmony_ci`NODE_REPL_HISTORY=''`.
7061cb0ef41Sopenharmony_ci
7071cb0ef41Sopenharmony_ci### Using the Node.js REPL with advanced line-editors
7081cb0ef41Sopenharmony_ci
7091cb0ef41Sopenharmony_ciFor advanced line-editors, start Node.js with the environment variable
7101cb0ef41Sopenharmony_ci`NODE_NO_READLINE=1`. This will start the main and debugger REPL in canonical
7111cb0ef41Sopenharmony_citerminal settings, which will allow use with `rlwrap`.
7121cb0ef41Sopenharmony_ci
7131cb0ef41Sopenharmony_ciFor example, the following can be added to a `.bashrc` file:
7141cb0ef41Sopenharmony_ci
7151cb0ef41Sopenharmony_ci```text
7161cb0ef41Sopenharmony_cialias node="env NODE_NO_READLINE=1 rlwrap node"
7171cb0ef41Sopenharmony_ci```
7181cb0ef41Sopenharmony_ci
7191cb0ef41Sopenharmony_ci### Starting multiple REPL instances against a single running instance
7201cb0ef41Sopenharmony_ci
7211cb0ef41Sopenharmony_ciIt is possible to create and run multiple REPL instances against a single
7221cb0ef41Sopenharmony_cirunning instance of Node.js that share a single `global` object but have
7231cb0ef41Sopenharmony_ciseparate I/O interfaces.
7241cb0ef41Sopenharmony_ci
7251cb0ef41Sopenharmony_ciThe following example, for instance, provides separate REPLs on `stdin`, a Unix
7261cb0ef41Sopenharmony_cisocket, and a TCP socket:
7271cb0ef41Sopenharmony_ci
7281cb0ef41Sopenharmony_ci```js
7291cb0ef41Sopenharmony_ciconst net = require('node:net');
7301cb0ef41Sopenharmony_ciconst repl = require('node:repl');
7311cb0ef41Sopenharmony_cilet connections = 0;
7321cb0ef41Sopenharmony_ci
7331cb0ef41Sopenharmony_cirepl.start({
7341cb0ef41Sopenharmony_ci  prompt: 'Node.js via stdin> ',
7351cb0ef41Sopenharmony_ci  input: process.stdin,
7361cb0ef41Sopenharmony_ci  output: process.stdout,
7371cb0ef41Sopenharmony_ci});
7381cb0ef41Sopenharmony_ci
7391cb0ef41Sopenharmony_cinet.createServer((socket) => {
7401cb0ef41Sopenharmony_ci  connections += 1;
7411cb0ef41Sopenharmony_ci  repl.start({
7421cb0ef41Sopenharmony_ci    prompt: 'Node.js via Unix socket> ',
7431cb0ef41Sopenharmony_ci    input: socket,
7441cb0ef41Sopenharmony_ci    output: socket,
7451cb0ef41Sopenharmony_ci  }).on('exit', () => {
7461cb0ef41Sopenharmony_ci    socket.end();
7471cb0ef41Sopenharmony_ci  });
7481cb0ef41Sopenharmony_ci}).listen('/tmp/node-repl-sock');
7491cb0ef41Sopenharmony_ci
7501cb0ef41Sopenharmony_cinet.createServer((socket) => {
7511cb0ef41Sopenharmony_ci  connections += 1;
7521cb0ef41Sopenharmony_ci  repl.start({
7531cb0ef41Sopenharmony_ci    prompt: 'Node.js via TCP socket> ',
7541cb0ef41Sopenharmony_ci    input: socket,
7551cb0ef41Sopenharmony_ci    output: socket,
7561cb0ef41Sopenharmony_ci  }).on('exit', () => {
7571cb0ef41Sopenharmony_ci    socket.end();
7581cb0ef41Sopenharmony_ci  });
7591cb0ef41Sopenharmony_ci}).listen(5001);
7601cb0ef41Sopenharmony_ci```
7611cb0ef41Sopenharmony_ci
7621cb0ef41Sopenharmony_ciRunning this application from the command line will start a REPL on stdin.
7631cb0ef41Sopenharmony_ciOther REPL clients may connect through the Unix socket or TCP socket. `telnet`,
7641cb0ef41Sopenharmony_cifor instance, is useful for connecting to TCP sockets, while `socat` can be used
7651cb0ef41Sopenharmony_cito connect to both Unix and TCP sockets.
7661cb0ef41Sopenharmony_ci
7671cb0ef41Sopenharmony_ciBy starting a REPL from a Unix socket-based server instead of stdin, it is
7681cb0ef41Sopenharmony_cipossible to connect to a long-running Node.js process without restarting it.
7691cb0ef41Sopenharmony_ci
7701cb0ef41Sopenharmony_ciFor an example of running a "full-featured" (`terminal`) REPL over
7711cb0ef41Sopenharmony_cia `net.Server` and `net.Socket` instance, see:
7721cb0ef41Sopenharmony_ci<https://gist.github.com/TooTallNate/2209310>.
7731cb0ef41Sopenharmony_ci
7741cb0ef41Sopenharmony_ciFor an example of running a REPL instance over [`curl(1)`][], see:
7751cb0ef41Sopenharmony_ci<https://gist.github.com/TooTallNate/2053342>.
7761cb0ef41Sopenharmony_ci
7771cb0ef41Sopenharmony_ci[TTY keybindings]: readline.md#tty-keybindings
7781cb0ef41Sopenharmony_ci[ZSH]: https://en.wikipedia.org/wiki/Z_shell
7791cb0ef41Sopenharmony_ci[`'uncaughtException'`]: process.md#event-uncaughtexception
7801cb0ef41Sopenharmony_ci[`--no-experimental-repl-await`]: cli.md#--no-experimental-repl-await
7811cb0ef41Sopenharmony_ci[`ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE`]: errors.md#err_domain_cannot_set_uncaught_exception_capture
7821cb0ef41Sopenharmony_ci[`ERR_INVALID_REPL_INPUT`]: errors.md#err_invalid_repl_input
7831cb0ef41Sopenharmony_ci[`curl(1)`]: https://curl.haxx.se/docs/manpage.html
7841cb0ef41Sopenharmony_ci[`domain`]: domain.md
7851cb0ef41Sopenharmony_ci[`process.setUncaughtExceptionCaptureCallback()`]: process.md#processsetuncaughtexceptioncapturecallbackfn
7861cb0ef41Sopenharmony_ci[`readline.InterfaceCompleter`]: readline.md#use-of-the-completer-function
7871cb0ef41Sopenharmony_ci[`repl.ReplServer`]: #class-replserver
7881cb0ef41Sopenharmony_ci[`repl.start()`]: #replstartoptions
7891cb0ef41Sopenharmony_ci[`reverse-i-search`]: #reverse-i-search
7901cb0ef41Sopenharmony_ci[`util.inspect()`]: util.md#utilinspectobject-options
7911cb0ef41Sopenharmony_ci[stream]: stream.md
792