• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v14.21.2">
7  <title>REPL | Node.js v14.21.2 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/repl.html">
12</head>
13<body class="alt apidoc" id="api-section-repl">
14  <div id="content" class="clearfix">
15    <div id="column2" class="interior">
16      <div id="intro" class="interior">
17        <a href="/" title="Go back to the home page">
18          Node.js
19        </a>
20      </div>
21      <ul>
22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
24</ul>
25<hr class="line">
26<ul>
27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
30<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36<li><a href="console.html" class="nav-console">Console</a></li>
37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42<li><a href="dns.html" class="nav-dns">DNS</a></li>
43<li><a href="domain.html" class="nav-domain">Domain</a></li>
44<li><a href="errors.html" class="nav-errors">Errors</a></li>
45<li><a href="events.html" class="nav-events">Events</a></li>
46<li><a href="fs.html" class="nav-fs">File system</a></li>
47<li><a href="globals.html" class="nav-globals">Globals</a></li>
48<li><a href="http.html" class="nav-http">HTTP</a></li>
49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
50<li><a href="https.html" class="nav-https">HTTPS</a></li>
51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
57<li><a href="net.html" class="nav-net">Net</a></li>
58<li><a href="os.html" class="nav-os">OS</a></li>
59<li><a href="path.html" class="nav-path">Path</a></li>
60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61<li><a href="policy.html" class="nav-policy">Policies</a></li>
62<li><a href="process.html" class="nav-process">Process</a></li>
63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65<li><a href="readline.html" class="nav-readline">Readline</a></li>
66<li><a href="repl.html" class="nav-repl active">REPL</a></li>
67<li><a href="report.html" class="nav-report">Report</a></li>
68<li><a href="stream.html" class="nav-stream">Stream</a></li>
69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70<li><a href="timers.html" class="nav-timers">Timers</a></li>
71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73<li><a href="tty.html" class="nav-tty">TTY</a></li>
74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75<li><a href="url.html" class="nav-url">URL</a></li>
76<li><a href="util.html" class="nav-util">Utilities</a></li>
77<li><a href="v8.html" class="nav-v8">V8</a></li>
78<li><a href="vm.html" class="nav-vm">VM</a></li>
79<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
82</ul>
83<hr class="line">
84<ul>
85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
86</ul>
87    </div>
88
89    <div id="column1" data-id="repl" class="interior">
90      <header>
91        <div class="header-container">
92          <h1>Node.js v14.21.2 documentation</h1>
93          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
94            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
95              <path fill="none" d="M0 0h24v24H0z" />
96              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
97              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
98            </svg>
99            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
100              <path d="M0 0h24v24H0z" fill="none" />
101              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
102            </svg>
103          </button>
104        </div>
105        <div id="gtoc">
106          <ul>
107            <li>
108              <a href="index.html">Index</a>
109            </li>
110            <li>
111              <a href="all.html">View on single page</a>
112            </li>
113            <li>
114              <a href="repl.json">View as JSON</a>
115            </li>
116
117    <li class="version-picker">
118      <a href="#">View another version <span>&#x25bc;</span></a>
119      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v19.x/api/repl.html">19.x</a></li>
120<li><a href="https://nodejs.org/docs/latest-v18.x/api/repl.html">18.x <b>LTS</b></a></li>
121<li><a href="https://nodejs.org/docs/latest-v17.x/api/repl.html">17.x</a></li>
122<li><a href="https://nodejs.org/docs/latest-v16.x/api/repl.html">16.x <b>LTS</b></a></li>
123<li><a href="https://nodejs.org/docs/latest-v15.x/api/repl.html">15.x</a></li>
124<li><a href="https://nodejs.org/docs/latest-v14.x/api/repl.html">14.x <b>LTS</b></a></li>
125<li><a href="https://nodejs.org/docs/latest-v13.x/api/repl.html">13.x</a></li>
126<li><a href="https://nodejs.org/docs/latest-v12.x/api/repl.html">12.x</a></li>
127<li><a href="https://nodejs.org/docs/latest-v11.x/api/repl.html">11.x</a></li>
128<li><a href="https://nodejs.org/docs/latest-v10.x/api/repl.html">10.x</a></li>
129<li><a href="https://nodejs.org/docs/latest-v9.x/api/repl.html">9.x</a></li>
130<li><a href="https://nodejs.org/docs/latest-v8.x/api/repl.html">8.x</a></li>
131<li><a href="https://nodejs.org/docs/latest-v7.x/api/repl.html">7.x</a></li>
132<li><a href="https://nodejs.org/docs/latest-v6.x/api/repl.html">6.x</a></li>
133<li><a href="https://nodejs.org/docs/latest-v5.x/api/repl.html">5.x</a></li>
134<li><a href="https://nodejs.org/docs/latest-v4.x/api/repl.html">4.x</a></li>
135<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/repl.html">0.12.x</a></li>
136<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/repl.html">0.10.x</a></li></ol>
137    </li>
138
139            <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/repl.md">Edit on GitHub</a></li>
140          </ul>
141        </div>
142        <hr>
143      </header>
144
145      <details id="toc" open><summary>Table of contents</summary><ul>
146<li><span class="stability_2"><a href="#repl_repl">REPL</a></span>
147<ul>
148<li><a href="#repl_design_and_features">Design and features</a>
149<ul>
150<li><a href="#repl_commands_and_special_keys">Commands and special keys</a></li>
151<li><a href="#repl_default_evaluation">Default evaluation</a>
152<ul>
153<li><a href="#repl_javascript_expressions">JavaScript expressions</a></li>
154<li><a href="#repl_global_and_local_scope">Global and local scope</a></li>
155<li><a href="#repl_accessing_core_node_js_modules">Accessing core Node.js modules</a></li>
156<li><a href="#repl_global_uncaught_exceptions">Global uncaught exceptions</a></li>
157<li><a href="#repl_assignment_of_the_underscore_variable">Assignment of the <code>_</code> (underscore) variable</a></li>
158<li><a href="#repl_await_keyword"><code>await</code> keyword</a></li>
159</ul>
160</li>
161<li><a href="#repl_reverse_i_search">Reverse-i-search</a></li>
162<li><a href="#repl_custom_evaluation_functions">Custom evaluation functions</a>
163<ul>
164<li><a href="#repl_recoverable_errors">Recoverable errors</a></li>
165</ul>
166</li>
167<li><a href="#repl_customizing_repl_output">Customizing REPL output</a></li>
168</ul>
169</li>
170<li><a href="#repl_class_replserver">Class: <code>REPLServer</code></a>
171<ul>
172<li><a href="#repl_event_exit">Event: <code>'exit'</code></a></li>
173<li><a href="#repl_event_reset">Event: <code>'reset'</code></a></li>
174<li><a href="#repl_replserver_definecommand_keyword_cmd"><code>replServer.defineCommand(keyword, cmd)</code></a></li>
175<li><a href="#repl_replserver_displayprompt_preservecursor"><code>replServer.displayPrompt([preserveCursor])</code></a></li>
176<li><a href="#repl_replserver_clearbufferedcommand"><code>replServer.clearBufferedCommand()</code></a></li>
177<li><span class="stability_0"><a href="#repl_replserver_parsereplkeyword_keyword_rest"><code>replServer.parseREPLKeyword(keyword[, rest])</code></a></span></li>
178<li><a href="#repl_replserver_setuphistory_historypath_callback"><code>replServer.setupHistory(historyPath, callback)</code></a></li>
179</ul>
180</li>
181<li><a href="#repl_repl_builtinmodules"><code>repl.builtinModules</code></a></li>
182<li><a href="#repl_repl_start_options"><code>repl.start([options])</code></a></li>
183<li><a href="#repl_the_node_js_repl">The Node.js REPL</a>
184<ul>
185<li><a href="#repl_environment_variable_options">Environment variable options</a></li>
186<li><a href="#repl_persistent_history">Persistent history</a></li>
187<li><a href="#repl_using_the_node_js_repl_with_advanced_line_editors">Using the Node.js REPL with advanced line-editors</a></li>
188<li><a href="#repl_starting_multiple_repl_instances_against_a_single_running_instance">Starting multiple REPL instances against a single running instance</a></li>
189</ul>
190</li>
191</ul>
192</li>
193</ul></details>
194
195      <div id="apicontent">
196        <h2>REPL<span><a class="mark" href="#repl_repl" id="repl_repl">#</a></span></h2>
197
198<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
199<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.21.2/lib/repl.js">lib/repl.js</a></p>
200<p>The <code>repl</code> module provides a Read-Eval-Print-Loop (REPL) implementation that
201is available both as a standalone program or includible in other applications.
202It can be accessed using:</p>
203<pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'repl'</span>);</code></pre>
204<section><h3>Design and features<span><a class="mark" href="#repl_design_and_features" id="repl_design_and_features">#</a></span></h3>
205<p>The <code>repl</code> module exports the <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> class. While running,
206instances of <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> will accept individual lines of user input,
207evaluate those according to a user-defined evaluation function, then output the
208result. Input and output may be from <code>stdin</code> and <code>stdout</code>, respectively, or may
209be connected to any Node.js <a href="stream.html">stream</a>.</p>
210<p>Instances of <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> support automatic completion of inputs,
211completion preview, simplistic Emacs-style line editing, multi-line inputs,
212<a href="https://en.wikipedia.org/wiki/Z_shell">ZSH</a>-like reverse-i-search, <a href="https://en.wikipedia.org/wiki/Z_shell">ZSH</a>-like substring-based history search,
213ANSI-styled output, saving and restoring current REPL session state, error
214recovery, and customizable evaluation functions. Terminals that do not support
215ANSI styles and Emacs-style line editing automatically fall back to a limited
216feature set.</p>
217<h4>Commands and special keys<span><a class="mark" href="#repl_commands_and_special_keys" id="repl_commands_and_special_keys">#</a></span></h4>
218<p>The following special commands are supported by all REPL instances:</p>
219<ul>
220<li><code>.break</code>: When in the process of inputting a multi-line expression, enter
221the <code>.break</code> command (or press <kbd>Ctrl</kbd>+<kbd>C</kbd>) to abort
222further input or processing of that expression.</li>
223<li><code>.clear</code>: Resets the REPL <code>context</code> to an empty object and clears any
224multi-line expression being input.</li>
225<li><code>.exit</code>: Close the I/O stream, causing the REPL to exit.</li>
226<li><code>.help</code>: Show this list of special commands.</li>
227<li><code>.save</code>: Save the current REPL session to a file:
228<code>> .save ./file/to/save.js</code></li>
229<li><code>.load</code>: Load a file into the current REPL session.
230<code>> .load ./file/to/load.js</code></li>
231<li><code>.editor</code>: Enter editor mode (<kbd>Ctrl</kbd>+<kbd>D</kbd> to finish,
232<kbd>Ctrl</kbd>+<kbd>C</kbd> to cancel).</li>
233</ul>
234<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">.editor</span>
235// Entering editor mode (^D to finish, ^C to cancel)
236function welcome(name) {
237  return `Hello ${name}!`;
238}
239
240welcome('Node.js User');
241
242// ^D
243'Hello Node.js User!'
244<span class="hljs-meta">></span></code></pre>
245<p>The following key combinations in the REPL have these special effects:</p>
246<ul>
247<li><kbd>Ctrl</kbd>+<kbd>C</kbd>: When pressed once, has the same effect as the
248<code>.break</code> command.
249When pressed twice on a blank line, has the same effect as the <code>.exit</code>
250command.</li>
251<li><kbd>Ctrl</kbd>+<kbd>D</kbd>: Has the same effect as the <code>.exit</code> command.</li>
252<li><kbd>Tab</kbd>: When pressed on a blank line, displays global and local (scope)
253variables. When pressed while entering other input, displays relevant
254autocompletion options.</li>
255</ul>
256<p>For key bindings related to the reverse-i-search, see <a href="#repl_reverse_i_search"><code>reverse-i-search</code></a>.
257For all other key bindings, see <a href="readline.html#readline_tty_keybindings">TTY keybindings</a>.</p>
258<h4>Default evaluation<span><a class="mark" href="#repl_default_evaluation" id="repl_default_evaluation">#</a></span></h4>
259<p>By default, all instances of <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> use an evaluation function
260that evaluates JavaScript expressions and provides access to Node.js built-in
261modules. This default behavior can be overridden by passing in an alternative
262evaluation function when the <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> instance is created.</p>
263<h5>JavaScript expressions<span><a class="mark" href="#repl_javascript_expressions" id="repl_javascript_expressions">#</a></span></h5>
264<p>The default evaluator supports direct evaluation of JavaScript expressions:</p>
265<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">1 + 1</span>
2662
267<span class="hljs-meta">> </span><span class="language-bash">const m = 2</span>
268undefined
269<span class="hljs-meta">> </span><span class="language-bash">m + 1</span>
2703</code></pre>
271<p>Unless otherwise scoped within blocks or functions, variables declared
272either implicitly or using the <code>const</code>, <code>let</code>, or <code>var</code> keywords
273are declared at the global scope.</p>
274<h5>Global and local scope<span><a class="mark" href="#repl_global_and_local_scope" id="repl_global_and_local_scope">#</a></span></h5>
275<p>The default evaluator provides access to any variables that exist in the global
276scope. It is possible to expose a variable to the REPL explicitly by assigning
277it to the <code>context</code> object associated with each <code>REPLServer</code>:</p>
278<pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'repl'</span>);
279<span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>;
280
281repl.<span class="hljs-title function_">start</span>(<span class="hljs-string">'> '</span>).<span class="hljs-property">context</span>.<span class="hljs-property">m</span> = msg;</code></pre>
282<p>Properties in the <code>context</code> object appear as local within the REPL:</p>
283<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node repl_test.js</span>
284<span class="hljs-meta">> </span><span class="language-bash">m</span>
285'message'</code></pre>
286<p>Context properties are not read-only by default. To specify read-only globals,
287context properties must be defined using <code>Object.defineProperty()</code>:</p>
288<pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'repl'</span>);
289<span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>;
290
291<span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>(<span class="hljs-string">'> '</span>);
292<span class="hljs-title class_">Object</span>.<span class="hljs-title function_">defineProperty</span>(r.<span class="hljs-property">context</span>, <span class="hljs-string">'m'</span>, {
293  <span class="hljs-attr">configurable</span>: <span class="hljs-literal">false</span>,
294  <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span>,
295  <span class="hljs-attr">value</span>: msg
296});</code></pre>
297<h5>Accessing core Node.js modules<span><a class="mark" href="#repl_accessing_core_node_js_modules" id="repl_accessing_core_node_js_modules">#</a></span></h5>
298<p>The default evaluator will automatically load Node.js core modules into the
299REPL environment when used. For instance, unless otherwise declared as a
300global or scoped variable, the input <code>fs</code> will be evaluated on-demand as
301<code>global.fs = require('fs')</code>.</p>
302<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">fs.createReadStream(<span class="hljs-string">'./some/file'</span>);</span></code></pre>
303<h5>Global uncaught exceptions<span><a class="mark" href="#repl_global_uncaught_exceptions" id="repl_global_uncaught_exceptions">#</a></span></h5>
304<div class="api_metadata">
305<details class="changelog"><summary>History</summary>
306<table>
307<tbody><tr><th>Version</th><th>Changes</th></tr>
308<tr><td>v12.3.0</td>
309<td><p>The <code>'uncaughtException'</code> event is from now on triggered if the repl is used as standalone program.</p></td></tr>
310</tbody></table>
311</details>
312</div>
313<p>The REPL uses the <a href="domain.html"><code>domain</code></a> module to catch all uncaught exceptions for that
314REPL session.</p>
315<p>This use of the <a href="domain.html"><code>domain</code></a> module in the REPL has these side effects:</p>
316<ul>
317<li>
318<p>Uncaught exceptions only emit the <a href="process.html#process_event_uncaughtexception"><code>'uncaughtException'</code></a> event in the
319standalone REPL. Adding a listener for this event in a REPL within
320another Node.js program results in <a href="errors.html#errors_err_invalid_repl_input"><code>ERR_INVALID_REPL_INPUT</code></a>.</p>
321<pre><code class="language-js"><span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>();
322
323r.<span class="hljs-title function_">write</span>(<span class="hljs-string">'process.on("uncaughtException", () => console.log("Foobar"));\n'</span>);
324<span class="hljs-comment">// Output stream includes:</span>
325<span class="hljs-comment">//   TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtException`</span>
326<span class="hljs-comment">//   cannot be used in the REPL</span>
327
328r.<span class="hljs-title function_">close</span>();</code></pre>
329</li>
330<li>
331<p>Trying to use <a href="process.html#process_process_setuncaughtexceptioncapturecallback_fn"><code>process.setUncaughtExceptionCaptureCallback()</code></a> throws
332an <a href="errors.html#errors_err_domain_cannot_set_uncaught_exception_capture"><code>ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE</code></a> error.</p>
333</li>
334</ul>
335<h5>Assignment of the <code>_</code> (underscore) variable<span><a class="mark" href="#repl_assignment_of_the_underscore_variable" id="repl_assignment_of_the_underscore_variable">#</a></span></h5>
336<div class="api_metadata">
337<details class="changelog"><summary>History</summary>
338<table>
339<tbody><tr><th>Version</th><th>Changes</th></tr>
340<tr><td>v9.8.0</td>
341<td><p>Added <code>_error</code> support.</p></td></tr>
342</tbody></table>
343</details>
344</div>
345<p>The default evaluator will, by default, assign the result of the most recently
346evaluated expression to the special variable <code>_</code> (underscore).
347Explicitly setting <code>_</code> to a value will disable this behavior.</p>
348<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">[ <span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span> ]</span>
349[ 'a', 'b', 'c' ]
350<span class="hljs-meta">> </span><span class="language-bash">_.length</span>
3513
352<span class="hljs-meta">> </span><span class="language-bash">_ += 1</span>
353Expression assignment to _ now disabled.
3544
355<span class="hljs-meta">> </span><span class="language-bash">1 + 1</span>
3562
357<span class="hljs-meta">> </span><span class="language-bash">_</span>
3584</code></pre>
359<p>Similarly, <code>_error</code> will refer to the last seen error, if there was any.
360Explicitly setting <code>_error</code> to a value will disable this behavior.</p>
361<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">throw new Error(<span class="hljs-string">'foo'</span>);</span>
362Error: foo
363<span class="hljs-meta">> </span><span class="language-bash">_error.message</span>
364'foo'</code></pre>
365<h5><code>await</code> keyword<span><a class="mark" href="#repl_await_keyword" id="repl_await_keyword">#</a></span></h5>
366<p>With the <a href="cli.html#cli_experimental_repl_await"><code>--experimental-repl-await</code></a> command-line option specified,
367experimental support for the <code>await</code> keyword is enabled.</p>
368<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">await Promise.resolve(123)</span>
369123
370<span class="hljs-meta">> </span><span class="language-bash">await Promise.reject(new Error(<span class="hljs-string">'REPL await'</span>))</span>
371Error: REPL await
372    at repl:1:45
373<span class="hljs-meta">> </span><span class="language-bash">const timeout = util.promisify(setTimeout);</span>
374undefined
375<span class="hljs-meta">> </span><span class="language-bash">const old = Date.now(); await timeout(1000); console.log(Date.now() - old);</span>
3761002
377undefined</code></pre>
378<p>One known limitation of using the <code>await</code> keyword in the REPL is that
379it will invalidate the lexical scoping of the <code>const</code> and <code>let</code>
380keywords.</p>
381<p>For example:</p>
382<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">const m = await Promise.resolve(123)</span>
383undefined
384<span class="hljs-meta">> </span><span class="language-bash">m</span>
385123
386<span class="hljs-meta">> </span><span class="language-bash">const m = await Promise.resolve(234)</span>
387undefined
388<span class="hljs-meta">> </span><span class="language-bash">m</span>
389234</code></pre>
390<h4>Reverse-i-search<span><a class="mark" href="#repl_reverse_i_search" id="repl_reverse_i_search">#</a></span></h4>
391<div class="api_metadata">
392<span>Added in: v13.6.0</span>
393</div>
394<p>The REPL supports bi-directional reverse-i-search similar to <a href="https://en.wikipedia.org/wiki/Z_shell">ZSH</a>. It is
395triggered with <kbd>Ctrl</kbd>+<kbd>R</kbd> to search backward and
396<kbd>Ctrl</kbd>+<kbd>S</kbd> to search
397forwards.</p>
398<p>Duplicated history entries will be skipped.</p>
399<p>Entries are accepted as soon as any key is pressed that doesn't correspond
400with the reverse search. Cancelling is possible by pressing <kbd>Esc</kbd> or
401<kbd>Ctrl</kbd>+<kbd>C</kbd>.</p>
402<p>Changing the direction immediately searches for the next entry in the expected
403direction from the current position on.</p>
404<h4>Custom evaluation functions<span><a class="mark" href="#repl_custom_evaluation_functions" id="repl_custom_evaluation_functions">#</a></span></h4>
405<p>When a new <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> is created, a custom evaluation function may be
406provided. This can be used, for instance, to implement fully customized REPL
407applications.</p>
408<p>The following illustrates a hypothetical example of a REPL that performs
409translation of text from one language to another:</p>
410<pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'repl'</span>);
411<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Translator</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'translator'</span>);
412
413<span class="hljs-keyword">const</span> myTranslator = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Translator</span>(<span class="hljs-string">'en'</span>, <span class="hljs-string">'fr'</span>);
414
415<span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span class="hljs-params">cmd, context, filename, callback</span>) {
416  <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, myTranslator.<span class="hljs-title function_">translate</span>(cmd));
417}
418
419repl.<span class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="hljs-string">'> '</span>, <span class="hljs-attr">eval</span>: myEval });</code></pre>
420<h5>Recoverable errors<span><a class="mark" href="#repl_recoverable_errors" id="repl_recoverable_errors">#</a></span></h5>
421<p>At the REPL prompt, pressing <kbd>Enter</kbd> sends the current line of input to
422the <code>eval</code> function. In order to support multi-line input, the <code>eval</code> function
423can return an instance of <code>repl.Recoverable</code> to the provided callback function:</p>
424<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span class="hljs-params">cmd, context, filename, callback</span>) {
425  <span class="hljs-keyword">let</span> result;
426  <span class="hljs-keyword">try</span> {
427    result = vm.<span class="hljs-title function_">runInThisContext</span>(cmd);
428  } <span class="hljs-keyword">catch</span> (e) {
429    <span class="hljs-keyword">if</span> (<span class="hljs-title function_">isRecoverableError</span>(e)) {
430      <span class="hljs-keyword">return</span> <span class="hljs-title function_">callback</span>(<span class="hljs-keyword">new</span> repl.<span class="hljs-title class_">Recoverable</span>(e));
431    }
432  }
433  <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, result);
434}
435
436<span class="hljs-keyword">function</span> <span class="hljs-title function_">isRecoverableError</span>(<span class="hljs-params">error</span>) {
437  <span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span class="hljs-string">'SyntaxError'</span>) {
438    <span class="hljs-keyword">return</span> <span class="hljs-regexp">/^(Unexpected end of input|Unexpected token)/</span>.<span class="hljs-title function_">test</span>(error.<span class="hljs-property">message</span>);
439  }
440  <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
441}</code></pre>
442<h4>Customizing REPL output<span><a class="mark" href="#repl_customizing_repl_output" id="repl_customizing_repl_output">#</a></span></h4>
443<p>By default, <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> instances format output using the
444<a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> method before writing the output to the provided <code>Writable</code>
445stream (<code>process.stdout</code> by default). The <code>showProxy</code> inspection option is set
446to true by default and the <code>colors</code> option is set to true depending on the
447REPL's <code>useColors</code> option.</p>
448<p>The <code>useColors</code> boolean option can be specified at construction to instruct the
449default writer to use ANSI style codes to colorize the output from the
450<code>util.inspect()</code> method.</p>
451<p>If the REPL is run as standalone program, it is also possible to change the
452REPL's <a href="util.html#util_util_inspect_object_options">inspection defaults</a> from inside the REPL by using the
453<code>inspect.replDefaults</code> property which mirrors the <code>defaultOptions</code> from
454<a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a>.</p>
455<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">util.inspect.replDefaults.compact = <span class="hljs-literal">false</span>;</span>
456false
457<span class="hljs-meta">> </span><span class="language-bash">[1]</span>
458[
459  1
460]
461<span class="hljs-meta">></span></code></pre>
462<p>To fully customize the output of a <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> instance pass in a new
463function for the <code>writer</code> option on construction. The following example, for
464instance, simply converts any input text to upper case:</p>
465<pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'repl'</span>);
466
467<span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="hljs-string">'> '</span>, <span class="hljs-attr">eval</span>: myEval, <span class="hljs-attr">writer</span>: myWriter });
468
469<span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span class="hljs-params">cmd, context, filename, callback</span>) {
470  <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, cmd);
471}
472
473<span class="hljs-keyword">function</span> <span class="hljs-title function_">myWriter</span>(<span class="hljs-params">output</span>) {
474  <span class="hljs-keyword">return</span> output.<span class="hljs-title function_">toUpperCase</span>();
475}</code></pre>
476</section><section><h3>Class: <code>REPLServer</code><span><a class="mark" href="#repl_class_replserver" id="repl_class_replserver">#</a></span></h3>
477<div class="api_metadata">
478<span>Added in: v0.1.91</span>
479</div>
480<ul>
481<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#repl_repl_start_options"><code>repl.start()</code></a></li>
482<li>Extends: <a href="readline.html#readline_class_interface" class="type">&#x3C;readline.Interface></a></li>
483</ul>
484<p>Instances of <code>repl.REPLServer</code> are created using the <a href="#repl_repl_start_options"><code>repl.start()</code></a> method
485or directly using the JavaScript <code>new</code> keyword.</p>
486<pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'repl'</span>);
487
488<span class="hljs-keyword">const</span> options = { <span class="hljs-attr">useColors</span>: <span class="hljs-literal">true</span> };
489
490<span class="hljs-keyword">const</span> firstInstance = repl.<span class="hljs-title function_">start</span>(options);
491<span class="hljs-keyword">const</span> secondInstance = <span class="hljs-keyword">new</span> repl.<span class="hljs-title function_">REPLServer</span>(options);</code></pre>
492<h4>Event: <code>'exit'</code><span><a class="mark" href="#repl_event_exit" id="repl_event_exit">#</a></span></h4>
493<div class="api_metadata">
494<span>Added in: v0.7.7</span>
495</div>
496<p>The <code>'exit'</code> event is emitted when the REPL is exited either by receiving the
497<code>.exit</code> command as input, the user pressing <kbd>Ctrl</kbd>+<kbd>C</kbd> twice
498to signal <code>SIGINT</code>,
499or by pressing <kbd>Ctrl</kbd>+<kbd>D</kbd> to signal <code>'end'</code> on the input
500stream. The listener
501callback is invoked without any arguments.</p>
502<pre><code class="language-js">replServer.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> {
503  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Received "exit" event from repl!'</span>);
504  process.<span class="hljs-title function_">exit</span>();
505});</code></pre>
506<h4>Event: <code>'reset'</code><span><a class="mark" href="#repl_event_reset" id="repl_event_reset">#</a></span></h4>
507<div class="api_metadata">
508<span>Added in: v0.11.0</span>
509</div>
510<p>The <code>'reset'</code> event is emitted when the REPL's context is reset. This occurs
511whenever the <code>.clear</code> command is received as input <em>unless</em> the REPL is using
512the default evaluator and the <code>repl.REPLServer</code> instance was created with the
513<code>useGlobal</code> option set to <code>true</code>. The listener callback will be called with a
514reference to the <code>context</code> object as the only argument.</p>
515<p>This can be used primarily to re-initialize REPL context to some pre-defined
516state:</p>
517<pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'repl'</span>);
518
519<span class="hljs-keyword">function</span> <span class="hljs-title function_">initializeContext</span>(<span class="hljs-params">context</span>) {
520  context.<span class="hljs-property">m</span> = <span class="hljs-string">'test'</span>;
521}
522
523<span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="hljs-string">'> '</span> });
524<span class="hljs-title function_">initializeContext</span>(r.<span class="hljs-property">context</span>);
525
526r.<span class="hljs-title function_">on</span>(<span class="hljs-string">'reset'</span>, initializeContext);</code></pre>
527<p>When this code is executed, the global <code>'m'</code> variable can be modified but then
528reset to its initial value using the <code>.clear</code> command:</p>
529<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">./node example.js</span>
530<span class="hljs-meta">> </span><span class="language-bash">m</span>
531'test'
532<span class="hljs-meta">> </span><span class="language-bash">m = 1</span>
5331
534<span class="hljs-meta">> </span><span class="language-bash">m</span>
5351
536<span class="hljs-meta">> </span><span class="language-bash">.clear</span>
537Clearing context...
538<span class="hljs-meta">> </span><span class="language-bash">m</span>
539'test'
540<span class="hljs-meta">></span></code></pre>
541<h4><code>replServer.defineCommand(keyword, cmd)</code><span><a class="mark" href="#repl_replserver_definecommand_keyword_cmd" id="repl_replserver_definecommand_keyword_cmd">#</a></span></h4>
542<div class="api_metadata">
543<span>Added in: v0.3.0</span>
544</div>
545<ul>
546<li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The command keyword (<em>without</em> a leading <code>.</code> character).</li>
547<li><code>cmd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The function to invoke when the command is processed.</li>
548</ul>
549<p>The <code>replServer.defineCommand()</code> method is used to add new <code>.</code>-prefixed commands
550to the REPL instance. Such commands are invoked by typing a <code>.</code> followed by the
551<code>keyword</code>. The <code>cmd</code> is either a <code>Function</code> or an <code>Object</code> with the following
552properties:</p>
553<ul>
554<li><code>help</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Help text to be displayed when <code>.help</code> is entered (Optional).</li>
555<li><code>action</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The function to execute, optionally accepting a single
556string argument.</li>
557</ul>
558<p>The following example shows two new commands added to the REPL instance:</p>
559<pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'repl'</span>);
560
561<span class="hljs-keyword">const</span> replServer = repl.<span class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="hljs-string">'> '</span> });
562replServer.<span class="hljs-title function_">defineCommand</span>(<span class="hljs-string">'sayhello'</span>, {
563  <span class="hljs-attr">help</span>: <span class="hljs-string">'Say hello'</span>,
564  <span class="hljs-title function_">action</span>(<span class="hljs-params">name</span>) {
565    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">clearBufferedCommand</span>();
566    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Hello, <span class="hljs-subst">${name}</span>!`</span>);
567    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">displayPrompt</span>();
568  }
569});
570replServer.<span class="hljs-title function_">defineCommand</span>(<span class="hljs-string">'saybye'</span>, <span class="hljs-keyword">function</span> <span class="hljs-title function_">saybye</span>(<span class="hljs-params"></span>) {
571  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Goodbye!'</span>);
572  <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">close</span>();
573});</code></pre>
574<p>The new commands can then be used from within the REPL instance:</p>
575<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">.sayhello Node.js User</span>
576Hello, Node.js User!
577<span class="hljs-meta">> </span><span class="language-bash">.saybye</span>
578Goodbye!</code></pre>
579<h4><code>replServer.displayPrompt([preserveCursor])</code><span><a class="mark" href="#repl_replserver_displayprompt_preservecursor" id="repl_replserver_displayprompt_preservecursor">#</a></span></h4>
580<div class="api_metadata">
581<span>Added in: v0.1.91</span>
582</div>
583<ul>
584<li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
585</ul>
586<p>The <code>replServer.displayPrompt()</code> method readies the REPL instance for input
587from the user, printing the configured <code>prompt</code> to a new line in the <code>output</code>
588and resuming the <code>input</code> to accept new input.</p>
589<p>When multi-line input is being entered, an ellipsis is printed rather than the
590'prompt'.</p>
591<p>When <code>preserveCursor</code> is <code>true</code>, the cursor placement will not be reset to <code>0</code>.</p>
592<p>The <code>replServer.displayPrompt</code> method is primarily intended to be called from
593within the action function for commands registered using the
594<code>replServer.defineCommand()</code> method.</p>
595<h4><code>replServer.clearBufferedCommand()</code><span><a class="mark" href="#repl_replserver_clearbufferedcommand" id="repl_replserver_clearbufferedcommand">#</a></span></h4>
596<div class="api_metadata">
597<span>Added in: v9.0.0</span>
598</div>
599<p>The <code>replServer.clearBufferedCommand()</code> method clears any command that has been
600buffered but not yet executed. This method is primarily intended to be
601called from within the action function for commands registered using the
602<code>replServer.defineCommand()</code> method.</p>
603<h4><code>replServer.parseREPLKeyword(keyword[, rest])</code><span><a class="mark" href="#repl_replserver_parsereplkeyword_keyword_rest" id="repl_replserver_parsereplkeyword_keyword_rest">#</a></span></h4>
604<div class="api_metadata">
605<span>Added in: v0.8.9</span><span>Deprecated since: v9.0.0</span>
606</div>
607<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated.</div><p></p>
608<ul>
609<li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> the potential keyword to parse and execute</li>
610<li><code>rest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> any parameters to the keyword command</li>
611<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
612</ul>
613<p>An internal method used to parse and execute <code>REPLServer</code> keywords.
614Returns <code>true</code> if <code>keyword</code> is a valid keyword, otherwise <code>false</code>.</p>
615<h4><code>replServer.setupHistory(historyPath, callback)</code><span><a class="mark" href="#repl_replserver_setuphistory_historypath_callback" id="repl_replserver_setuphistory_historypath_callback">#</a></span></h4>
616<div class="api_metadata">
617<span>Added in: v11.10.0</span>
618</div>
619<ul>
620<li><code>historyPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> the path to the history file</li>
621<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> called when history writes are ready or upon error
622<ul>
623<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
624<li><code>repl</code> <a href="repl.html#repl_class_replserver" class="type">&#x3C;repl.REPLServer></a></li>
625</ul>
626</li>
627</ul>
628<p>Initializes a history log file for the REPL instance. When executing the
629Node.js binary and using the command-line REPL, a history file is initialized
630by default. However, this is not the case when creating a REPL
631programmatically. Use this method to initialize a history log file when working
632with REPL instances programmatically.</p>
633</section><section><h3><code>repl.builtinModules</code><span><a class="mark" href="#repl_repl_builtinmodules" id="repl_repl_builtinmodules">#</a></span></h3>
634<div class="api_metadata">
635<span>Added in: v14.5.0</span>
636</div>
637<ul>
638<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
639</ul>
640<p>A list of the names of all Node.js modules, e.g., <code>'http'</code>.</p>
641</section><section><h3><code>repl.start([options])</code><span><a class="mark" href="#repl_repl_start_options" id="repl_repl_start_options">#</a></span></h3>
642<div class="api_metadata">
643<details class="changelog"><summary>History</summary>
644<table>
645<tbody><tr><th>Version</th><th>Changes</th></tr>
646<tr><td>v13.4.0</td>
647<td><p>The <code>preview</code> option is now available.</p></td></tr>
648<tr><td>v12.0.0</td>
649<td><p>The <code>terminal</code> option now follows the default description in all cases and <code>useColors</code> checks <code>hasColors()</code> if available.</p></td></tr>
650<tr><td>v10.0.0</td>
651<td><p>The <code>REPL_MAGIC_MODE</code> <code>replMode</code> was removed.</p></td></tr>
652<tr><td>v6.3.0</td>
653<td><p>The <code>breakEvalOnSigint</code> option is supported now.</p></td></tr>
654<tr><td>v5.8.0</td>
655<td><p>The <code>options</code> parameter is optional now.</p></td></tr>
656<tr><td>v0.1.91</td>
657<td><p><span>Added in: v0.1.91</span></p></td></tr>
658</tbody></table>
659</details>
660</div>
661<ul>
662<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
663<ul>
664<li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The input prompt to display. <strong>Default:</strong> <code>'> '</code>
665(with a trailing space).</li>
666<li><code>input</code> <a href="stream.html#stream_class_stream_readable" class="type">&#x3C;stream.Readable></a> The <code>Readable</code> stream from which REPL input will
667be read. <strong>Default:</strong> <code>process.stdin</code>.</li>
668<li><code>output</code> <a href="stream.html#stream_class_stream_writable" class="type">&#x3C;stream.Writable></a> The <code>Writable</code> stream to which REPL output will
669be written. <strong>Default:</strong> <code>process.stdout</code>.</li>
670<li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, specifies that the <code>output</code> should be
671treated as a TTY terminal.
672<strong>Default:</strong> checking the value of the <code>isTTY</code> property on the <code>output</code>
673stream upon instantiation.</li>
674<li><code>eval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The function to be used when evaluating each given line
675of input. <strong>Default:</strong> an async wrapper for the JavaScript <code>eval()</code>
676function. An <code>eval</code> function can error with <code>repl.Recoverable</code> to indicate
677the input was incomplete and prompt for additional lines.</li>
678<li><code>useColors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, specifies that the default <code>writer</code>
679function should include ANSI color styling to REPL output. If a custom
680<code>writer</code> function is provided then this has no effect. <strong>Default:</strong> checking
681color support on the <code>output</code> stream if the REPL instance's <code>terminal</code> value
682is <code>true</code>.</li>
683<li><code>useGlobal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, specifies that the default evaluation
684function will use the JavaScript <code>global</code> as the context as opposed to
685creating a new separate context for the REPL instance. The node CLI REPL
686sets this value to <code>true</code>. <strong>Default:</strong> <code>false</code>.</li>
687<li><code>ignoreUndefined</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, specifies that the default writer
688will not output the return value of a command if it evaluates to
689<code>undefined</code>. <strong>Default:</strong> <code>false</code>.</li>
690<li><code>writer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The function to invoke to format the output of each
691command before writing to <code>output</code>. <strong>Default:</strong> <a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a>.</li>
692<li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> An optional function used for custom Tab auto
693completion. See <a href="readline.html#readline_use_of_the_completer_function"><code>readline.InterfaceCompleter</code></a> for an example.</li>
694<li><code>replMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a> A flag that specifies whether the default evaluator
695executes all JavaScript commands in strict mode or default (sloppy) mode.
696Acceptable values are:
697<ul>
698<li><code>repl.REPL_MODE_SLOPPY</code> to evaluate expressions in sloppy mode.</li>
699<li><code>repl.REPL_MODE_STRICT</code> to evaluate expressions in strict mode. This is
700equivalent to prefacing every repl statement with <code>'use strict'</code>.</li>
701</ul>
702</li>
703<li><code>breakEvalOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Stop evaluating the current piece of code when
704<code>SIGINT</code> is received, such as when <kbd>Ctrl</kbd>+<kbd>C</kbd> is pressed.
705This cannot be used
706together with a custom <code>eval</code> function. <strong>Default:</strong> <code>false</code>.</li>
707<li><code>preview</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Defines if the repl prints autocomplete and output
708previews or not. <strong>Default:</strong> <code>true</code> with the default eval function and
709<code>false</code> in case a custom eval function is used. If <code>terminal</code> is falsy, then
710there are no previews and the value of <code>preview</code> has no effect.</li>
711</ul>
712</li>
713<li>Returns: <a href="repl.html#repl_class_replserver" class="type">&#x3C;repl.REPLServer></a></li>
714</ul>
715<p>The <code>repl.start()</code> method creates and starts a <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> instance.</p>
716<p>If <code>options</code> is a string, then it specifies the input prompt:</p>
717<pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'repl'</span>);
718
719<span class="hljs-comment">// a Unix style prompt</span>
720repl.<span class="hljs-title function_">start</span>(<span class="hljs-string">'$ '</span>);</code></pre>
721</section><section><h3>The Node.js REPL<span><a class="mark" href="#repl_the_node_js_repl" id="repl_the_node_js_repl">#</a></span></h3>
722<p>Node.js itself uses the <code>repl</code> module to provide its own interactive interface
723for executing JavaScript. This can be used by executing the Node.js binary
724without passing any arguments (or by passing the <code>-i</code> argument):</p>
725<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node</span>
726<span class="hljs-meta">> </span><span class="language-bash">const a = [1, 2, 3];</span>
727undefined
728<span class="hljs-meta">> </span><span class="language-bash">a</span>
729[ 1, 2, 3 ]
730<span class="hljs-meta">> </span><span class="language-bash">a.forEach((v) => {</span>
731...   console.log(v);
732...   });
7331
7342
7353</code></pre>
736<h4>Environment variable options<span><a class="mark" href="#repl_environment_variable_options" id="repl_environment_variable_options">#</a></span></h4>
737<p>Various behaviors of the Node.js REPL can be customized using the following
738environment variables:</p>
739<ul>
740<li><code>NODE_REPL_HISTORY</code>: When a valid path is given, persistent REPL history
741will be saved to the specified file rather than <code>.node_repl_history</code> in the
742user's home directory. Setting this value to <code>''</code> (an empty string) will
743disable persistent REPL history. Whitespace will be trimmed from the value.
744On Windows platforms environment variables with empty values are invalid so
745set this variable to one or more spaces to disable persistent REPL history.</li>
746<li><code>NODE_REPL_HISTORY_SIZE</code>: Controls how many lines of history will be
747persisted if history is available. Must be a positive number.
748<strong>Default:</strong> <code>1000</code>.</li>
749<li><code>NODE_REPL_MODE</code>: May be either <code>'sloppy'</code> or <code>'strict'</code>. <strong>Default:</strong>
750<code>'sloppy'</code>, which will allow non-strict mode code to be run.</li>
751</ul>
752<h4>Persistent history<span><a class="mark" href="#repl_persistent_history" id="repl_persistent_history">#</a></span></h4>
753<p>By default, the Node.js REPL will persist history between <code>node</code> REPL sessions
754by saving inputs to a <code>.node_repl_history</code> file located in the user's home
755directory. This can be disabled by setting the environment variable
756<code>NODE_REPL_HISTORY=''</code>.</p>
757<h4>Using the Node.js REPL with advanced line-editors<span><a class="mark" href="#repl_using_the_node_js_repl_with_advanced_line_editors" id="repl_using_the_node_js_repl_with_advanced_line_editors">#</a></span></h4>
758<p>For advanced line-editors, start Node.js with the environment variable
759<code>NODE_NO_READLINE=1</code>. This will start the main and debugger REPL in canonical
760terminal settings, which will allow use with <code>rlwrap</code>.</p>
761<p>For example, the following can be added to a <code>.bashrc</code> file:</p>
762<pre><code class="language-text">alias node="env NODE_NO_READLINE=1 rlwrap node"</code></pre>
763<h4>Starting multiple REPL instances against a single running instance<span><a class="mark" href="#repl_starting_multiple_repl_instances_against_a_single_running_instance" id="repl_starting_multiple_repl_instances_against_a_single_running_instance">#</a></span></h4>
764<p>It is possible to create and run multiple REPL instances against a single
765running instance of Node.js that share a single <code>global</code> object but have
766separate I/O interfaces.</p>
767<p>The following example, for instance, provides separate REPLs on <code>stdin</code>, a Unix
768socket, and a TCP socket:</p>
769<pre><code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>);
770<span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'repl'</span>);
771<span class="hljs-keyword">let</span> connections = <span class="hljs-number">0</span>;
772
773repl.<span class="hljs-title function_">start</span>({
774  <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via stdin> '</span>,
775  <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
776  <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>
777});
778
779net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> {
780  connections += <span class="hljs-number">1</span>;
781  repl.<span class="hljs-title function_">start</span>({
782    <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via Unix socket> '</span>,
783    <span class="hljs-attr">input</span>: socket,
784    <span class="hljs-attr">output</span>: socket
785  }).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> {
786    socket.<span class="hljs-title function_">end</span>();
787  });
788}).<span class="hljs-title function_">listen</span>(<span class="hljs-string">'/tmp/node-repl-sock'</span>);
789
790net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> {
791  connections += <span class="hljs-number">1</span>;
792  repl.<span class="hljs-title function_">start</span>({
793    <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via TCP socket> '</span>,
794    <span class="hljs-attr">input</span>: socket,
795    <span class="hljs-attr">output</span>: socket
796  }).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> {
797    socket.<span class="hljs-title function_">end</span>();
798  });
799}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">5001</span>);</code></pre>
800<p>Running this application from the command line will start a REPL on stdin.
801Other REPL clients may connect through the Unix socket or TCP socket. <code>telnet</code>,
802for instance, is useful for connecting to TCP sockets, while <code>socat</code> can be used
803to connect to both Unix and TCP sockets.</p>
804<p>By starting a REPL from a Unix socket-based server instead of stdin, it is
805possible to connect to a long-running Node.js process without restarting it.</p>
806<p>For an example of running a "full-featured" (<code>terminal</code>) REPL over
807a <code>net.Server</code> and <code>net.Socket</code> instance, see:
808<a href="https://gist.github.com/TooTallNate/2209310">https://gist.github.com/TooTallNate/2209310</a>.</p>
809<p>For an example of running a REPL instance over <a href="https://curl.haxx.se/docs/manpage.html"><code>curl(1)</code></a>, see:
810<a href="https://gist.github.com/TooTallNate/2053342">https://gist.github.com/TooTallNate/2053342</a>.</p></section>
811        <!-- API END -->
812      </div>
813    </div>
814  </div>
815  <script>
816    'use strict';
817    {
818      const kCustomPreference = 'customDarkTheme';
819      const userSettings = sessionStorage.getItem(kCustomPreference);
820      const themeToggleButton = document.getElementById('theme-toggle-btn');
821      if (userSettings === null && window.matchMedia) {
822        const mq = window.matchMedia('(prefers-color-scheme: dark)');
823        if ('onchange' in mq) {
824          function mqChangeListener(e) {
825            document.body.classList.toggle('dark-mode', e.matches);
826          }
827          mq.addEventListener('change', mqChangeListener);
828          if (themeToggleButton) {
829            themeToggleButton.addEventListener('click', function() {
830              mq.removeEventListener('change', mqChangeListener);
831            }, { once: true });
832          }
833        }
834        if (mq.matches) {
835          document.body.classList.add('dark-mode');
836        }
837      } else if (userSettings === 'true') {
838        document.body.classList.add('dark-mode');
839      }
840      if (themeToggleButton) {
841        themeToggleButton.hidden = false;
842        themeToggleButton.addEventListener('click', function() {
843          sessionStorage.setItem(
844            kCustomPreference,
845            document.body.classList.toggle('dark-mode')
846          );
847        });
848      }
849    }
850  </script>
851</body>
852</html>
853