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.19.1"> 7 <title>REPL | Node.js v14.19.1 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.19.1 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>▼</span></a> 119 <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v17.x/api/repl.html">17.x</a></li> 120<li><a href="https://nodejs.org/docs/latest-v16.x/api/repl.html">16.x <b>LTS</b></a></li> 121<li><a href="https://nodejs.org/docs/latest-v15.x/api/repl.html">15.x</a></li> 122<li><a href="https://nodejs.org/docs/latest-v14.x/api/repl.html">14.x <b>LTS</b></a></li> 123<li><a href="https://nodejs.org/docs/latest-v13.x/api/repl.html">13.x</a></li> 124<li><a href="https://nodejs.org/docs/latest-v12.x/api/repl.html">12.x <b>LTS</b></a></li> 125<li><a href="https://nodejs.org/docs/latest-v11.x/api/repl.html">11.x</a></li> 126<li><a href="https://nodejs.org/docs/latest-v10.x/api/repl.html">10.x</a></li> 127<li><a href="https://nodejs.org/docs/latest-v9.x/api/repl.html">9.x</a></li> 128<li><a href="https://nodejs.org/docs/latest-v8.x/api/repl.html">8.x</a></li> 129<li><a href="https://nodejs.org/docs/latest-v7.x/api/repl.html">7.x</a></li> 130<li><a href="https://nodejs.org/docs/latest-v6.x/api/repl.html">6.x</a></li> 131<li><a href="https://nodejs.org/docs/latest-v5.x/api/repl.html">5.x</a></li> 132<li><a href="https://nodejs.org/docs/latest-v4.x/api/repl.html">4.x</a></li> 133<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/repl.html">0.12.x</a></li> 134<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/repl.html">0.10.x</a></li></ol> 135 </li> 136 137 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/repl.md">Edit on GitHub</a></li> 138 </ul> 139 </div> 140 <hr> 141 </header> 142 143 <details id="toc" open><summary>Table of contents</summary><ul> 144<li><span class="stability_2"><a href="#repl_repl">REPL</a></span> 145<ul> 146<li><a href="#repl_design_and_features">Design and features</a> 147<ul> 148<li><a href="#repl_commands_and_special_keys">Commands and special keys</a></li> 149<li><a href="#repl_default_evaluation">Default evaluation</a> 150<ul> 151<li><a href="#repl_javascript_expressions">JavaScript expressions</a></li> 152<li><a href="#repl_global_and_local_scope">Global and local scope</a></li> 153<li><a href="#repl_accessing_core_node_js_modules">Accessing core Node.js modules</a></li> 154<li><a href="#repl_global_uncaught_exceptions">Global uncaught exceptions</a></li> 155<li><a href="#repl_assignment_of_the_underscore_variable">Assignment of the <code>_</code> (underscore) variable</a></li> 156<li><a href="#repl_await_keyword"><code>await</code> keyword</a></li> 157</ul> 158</li> 159<li><a href="#repl_reverse_i_search">Reverse-i-search</a></li> 160<li><a href="#repl_custom_evaluation_functions">Custom evaluation functions</a> 161<ul> 162<li><a href="#repl_recoverable_errors">Recoverable errors</a></li> 163</ul> 164</li> 165<li><a href="#repl_customizing_repl_output">Customizing REPL output</a></li> 166</ul> 167</li> 168<li><a href="#repl_class_replserver">Class: <code>REPLServer</code></a> 169<ul> 170<li><a href="#repl_event_exit">Event: <code>'exit'</code></a></li> 171<li><a href="#repl_event_reset">Event: <code>'reset'</code></a></li> 172<li><a href="#repl_replserver_definecommand_keyword_cmd"><code>replServer.defineCommand(keyword, cmd)</code></a></li> 173<li><a href="#repl_replserver_displayprompt_preservecursor"><code>replServer.displayPrompt([preserveCursor])</code></a></li> 174<li><a href="#repl_replserver_clearbufferedcommand"><code>replServer.clearBufferedCommand()</code></a></li> 175<li><span class="stability_0"><a href="#repl_replserver_parsereplkeyword_keyword_rest"><code>replServer.parseREPLKeyword(keyword[, rest])</code></a></span></li> 176<li><a href="#repl_replserver_setuphistory_historypath_callback"><code>replServer.setupHistory(historyPath, callback)</code></a></li> 177</ul> 178</li> 179<li><a href="#repl_repl_builtinmodules"><code>repl.builtinModules</code></a></li> 180<li><a href="#repl_repl_start_options"><code>repl.start([options])</code></a></li> 181<li><a href="#repl_the_node_js_repl">The Node.js REPL</a> 182<ul> 183<li><a href="#repl_environment_variable_options">Environment variable options</a></li> 184<li><a href="#repl_persistent_history">Persistent history</a></li> 185<li><a href="#repl_using_the_node_js_repl_with_advanced_line_editors">Using the Node.js REPL with advanced line-editors</a></li> 186<li><a href="#repl_starting_multiple_repl_instances_against_a_single_running_instance">Starting multiple REPL instances against a single running instance</a></li> 187</ul> 188</li> 189</ul> 190</li> 191</ul></details> 192 193 <div id="apicontent"> 194 <h2>REPL<span><a class="mark" href="#repl_repl" id="repl_repl">#</a></span></h2> 195 196<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> 197<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.19.1/lib/repl.js">lib/repl.js</a></p> 198<p>The <code>repl</code> module provides a Read-Eval-Print-Loop (REPL) implementation that 199is available both as a standalone program or includible in other applications. 200It can be accessed using:</p> 201<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> 202<section><h3>Design and features<span><a class="mark" href="#repl_design_and_features" id="repl_design_and_features">#</a></span></h3> 203<p>The <code>repl</code> module exports the <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> class. While running, 204instances of <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> will accept individual lines of user input, 205evaluate those according to a user-defined evaluation function, then output the 206result. Input and output may be from <code>stdin</code> and <code>stdout</code>, respectively, or may 207be connected to any Node.js <a href="stream.html">stream</a>.</p> 208<p>Instances of <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> support automatic completion of inputs, 209completion preview, simplistic Emacs-style line editing, multi-line inputs, 210<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, 211ANSI-styled output, saving and restoring current REPL session state, error 212recovery, and customizable evaluation functions. Terminals that do not support 213ANSI styles and Emacs-style line editing automatically fall back to a limited 214feature set.</p> 215<h4>Commands and special keys<span><a class="mark" href="#repl_commands_and_special_keys" id="repl_commands_and_special_keys">#</a></span></h4> 216<p>The following special commands are supported by all REPL instances:</p> 217<ul> 218<li><code>.break</code>: When in the process of inputting a multi-line expression, enter 219the <code>.break</code> command (or press <kbd>Ctrl</kbd>+<kbd>C</kbd>) to abort 220further input or processing of that expression.</li> 221<li><code>.clear</code>: Resets the REPL <code>context</code> to an empty object and clears any 222multi-line expression being input.</li> 223<li><code>.exit</code>: Close the I/O stream, causing the REPL to exit.</li> 224<li><code>.help</code>: Show this list of special commands.</li> 225<li><code>.save</code>: Save the current REPL session to a file: 226<code>> .save ./file/to/save.js</code></li> 227<li><code>.load</code>: Load a file into the current REPL session. 228<code>> .load ./file/to/load.js</code></li> 229<li><code>.editor</code>: Enter editor mode (<kbd>Ctrl</kbd>+<kbd>D</kbd> to finish, 230<kbd>Ctrl</kbd>+<kbd>C</kbd> to cancel).</li> 231</ul> 232<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">.editor</span> 233// Entering editor mode (^D to finish, ^C to cancel) 234function welcome(name) { 235 return `Hello ${name}!`; 236} 237 238welcome('Node.js User'); 239 240// ^D 241'Hello Node.js User!' 242<span class="hljs-meta">></span></code></pre> 243<p>The following key combinations in the REPL have these special effects:</p> 244<ul> 245<li><kbd>Ctrl</kbd>+<kbd>C</kbd>: When pressed once, has the same effect as the 246<code>.break</code> command. 247When pressed twice on a blank line, has the same effect as the <code>.exit</code> 248command.</li> 249<li><kbd>Ctrl</kbd>+<kbd>D</kbd>: Has the same effect as the <code>.exit</code> command.</li> 250<li><kbd>Tab</kbd>: When pressed on a blank line, displays global and local (scope) 251variables. When pressed while entering other input, displays relevant 252autocompletion options.</li> 253</ul> 254<p>For key bindings related to the reverse-i-search, see <a href="#repl_reverse_i_search"><code>reverse-i-search</code></a>. 255For all other key bindings, see <a href="readline.html#readline_tty_keybindings">TTY keybindings</a>.</p> 256<h4>Default evaluation<span><a class="mark" href="#repl_default_evaluation" id="repl_default_evaluation">#</a></span></h4> 257<p>By default, all instances of <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> use an evaluation function 258that evaluates JavaScript expressions and provides access to Node.js built-in 259modules. This default behavior can be overridden by passing in an alternative 260evaluation function when the <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> instance is created.</p> 261<h5>JavaScript expressions<span><a class="mark" href="#repl_javascript_expressions" id="repl_javascript_expressions">#</a></span></h5> 262<p>The default evaluator supports direct evaluation of JavaScript expressions:</p> 263<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">1 + 1</span> 2642 265<span class="hljs-meta">> </span><span class="language-bash">const m = 2</span> 266undefined 267<span class="hljs-meta">> </span><span class="language-bash">m + 1</span> 2683</code></pre> 269<p>Unless otherwise scoped within blocks or functions, variables declared 270either implicitly or using the <code>const</code>, <code>let</code>, or <code>var</code> keywords 271are declared at the global scope.</p> 272<h5>Global and local scope<span><a class="mark" href="#repl_global_and_local_scope" id="repl_global_and_local_scope">#</a></span></h5> 273<p>The default evaluator provides access to any variables that exist in the global 274scope. It is possible to expose a variable to the REPL explicitly by assigning 275it to the <code>context</code> object associated with each <code>REPLServer</code>:</p> 276<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>); 277<span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>; 278 279repl.<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> 280<p>Properties in the <code>context</code> object appear as local within the REPL:</p> 281<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node repl_test.js</span> 282<span class="hljs-meta">> </span><span class="language-bash">m</span> 283'message'</code></pre> 284<p>Context properties are not read-only by default. To specify read-only globals, 285context properties must be defined using <code>Object.defineProperty()</code>:</p> 286<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>); 287<span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>; 288 289<span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>(<span class="hljs-string">'> '</span>); 290<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>, { 291 <span class="hljs-attr">configurable</span>: <span class="hljs-literal">false</span>, 292 <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span>, 293 <span class="hljs-attr">value</span>: msg 294});</code></pre> 295<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> 296<p>The default evaluator will automatically load Node.js core modules into the 297REPL environment when used. For instance, unless otherwise declared as a 298global or scoped variable, the input <code>fs</code> will be evaluated on-demand as 299<code>global.fs = require('fs')</code>.</p> 300<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> 301<h5>Global uncaught exceptions<span><a class="mark" href="#repl_global_uncaught_exceptions" id="repl_global_uncaught_exceptions">#</a></span></h5> 302<div class="api_metadata"> 303<details class="changelog"><summary>History</summary> 304<table> 305<tbody><tr><th>Version</th><th>Changes</th></tr> 306<tr><td>v12.3.0</td> 307<td><p>The <code>'uncaughtException'</code> event is from now on triggered if the repl is used as standalone program.</p></td></tr> 308</tbody></table> 309</details> 310</div> 311<p>The REPL uses the <a href="domain.html"><code>domain</code></a> module to catch all uncaught exceptions for that 312REPL session.</p> 313<p>This use of the <a href="domain.html"><code>domain</code></a> module in the REPL has these side effects:</p> 314<ul> 315<li> 316<p>Uncaught exceptions only emit the <a href="process.html#process_event_uncaughtexception"><code>'uncaughtException'</code></a> event in the 317standalone REPL. Adding a listener for this event in a REPL within 318another Node.js program results in <a href="errors.html#errors_err_invalid_repl_input"><code>ERR_INVALID_REPL_INPUT</code></a>.</p> 319<pre><code class="language-js"><span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>(); 320 321r.<span class="hljs-title function_">write</span>(<span class="hljs-string">'process.on("uncaughtException", () => console.log("Foobar"));\n'</span>); 322<span class="hljs-comment">// Output stream includes:</span> 323<span class="hljs-comment">// TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtException`</span> 324<span class="hljs-comment">// cannot be used in the REPL</span> 325 326r.<span class="hljs-title function_">close</span>();</code></pre> 327</li> 328<li> 329<p>Trying to use <a href="process.html#process_process_setuncaughtexceptioncapturecallback_fn"><code>process.setUncaughtExceptionCaptureCallback()</code></a> throws 330an <a href="errors.html#errors_err_domain_cannot_set_uncaught_exception_capture"><code>ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE</code></a> error.</p> 331</li> 332</ul> 333<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> 334<div class="api_metadata"> 335<details class="changelog"><summary>History</summary> 336<table> 337<tbody><tr><th>Version</th><th>Changes</th></tr> 338<tr><td>v9.8.0</td> 339<td><p>Added <code>_error</code> support.</p></td></tr> 340</tbody></table> 341</details> 342</div> 343<p>The default evaluator will, by default, assign the result of the most recently 344evaluated expression to the special variable <code>_</code> (underscore). 345Explicitly setting <code>_</code> to a value will disable this behavior.</p> 346<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> 347[ 'a', 'b', 'c' ] 348<span class="hljs-meta">> </span><span class="language-bash">_.length</span> 3493 350<span class="hljs-meta">> </span><span class="language-bash">_ += 1</span> 351Expression assignment to _ now disabled. 3524 353<span class="hljs-meta">> </span><span class="language-bash">1 + 1</span> 3542 355<span class="hljs-meta">> </span><span class="language-bash">_</span> 3564</code></pre> 357<p>Similarly, <code>_error</code> will refer to the last seen error, if there was any. 358Explicitly setting <code>_error</code> to a value will disable this behavior.</p> 359<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">throw new Error(<span class="hljs-string">'foo'</span>);</span> 360Error: foo 361<span class="hljs-meta">> </span><span class="language-bash">_error.message</span> 362'foo'</code></pre> 363<h5><code>await</code> keyword<span><a class="mark" href="#repl_await_keyword" id="repl_await_keyword">#</a></span></h5> 364<p>With the <a href="cli.html#cli_experimental_repl_await"><code>--experimental-repl-await</code></a> command-line option specified, 365experimental support for the <code>await</code> keyword is enabled.</p> 366<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">await Promise.resolve(123)</span> 367123 368<span class="hljs-meta">> </span><span class="language-bash">await Promise.reject(new Error(<span class="hljs-string">'REPL await'</span>))</span> 369Error: REPL await 370 at repl:1:45 371<span class="hljs-meta">> </span><span class="language-bash">const timeout = util.promisify(setTimeout);</span> 372undefined 373<span class="hljs-meta">> </span><span class="language-bash">const old = Date.now(); await timeout(1000); console.log(Date.now() - old);</span> 3741002 375undefined</code></pre> 376<p>One known limitation of using the <code>await</code> keyword in the REPL is that 377it will invalidate the lexical scoping of the <code>const</code> and <code>let</code> 378keywords.</p> 379<p>For example:</p> 380<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">const m = await Promise.resolve(123)</span> 381undefined 382<span class="hljs-meta">> </span><span class="language-bash">m</span> 383123 384<span class="hljs-meta">> </span><span class="language-bash">const m = await Promise.resolve(234)</span> 385undefined 386<span class="hljs-meta">> </span><span class="language-bash">m</span> 387234</code></pre> 388<h4>Reverse-i-search<span><a class="mark" href="#repl_reverse_i_search" id="repl_reverse_i_search">#</a></span></h4> 389<div class="api_metadata"> 390<span>Added in: v13.6.0</span> 391</div> 392<p>The REPL supports bi-directional reverse-i-search similar to <a href="https://en.wikipedia.org/wiki/Z_shell">ZSH</a>. It is 393triggered with <kbd>Ctrl</kbd>+<kbd>R</kbd> to search backward and 394<kbd>Ctrl</kbd>+<kbd>S</kbd> to search 395forwards.</p> 396<p>Duplicated history entries will be skipped.</p> 397<p>Entries are accepted as soon as any key is pressed that doesn't correspond 398with the reverse search. Cancelling is possible by pressing <kbd>Esc</kbd> or 399<kbd>Ctrl</kbd>+<kbd>C</kbd>.</p> 400<p>Changing the direction immediately searches for the next entry in the expected 401direction from the current position on.</p> 402<h4>Custom evaluation functions<span><a class="mark" href="#repl_custom_evaluation_functions" id="repl_custom_evaluation_functions">#</a></span></h4> 403<p>When a new <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> is created, a custom evaluation function may be 404provided. This can be used, for instance, to implement fully customized REPL 405applications.</p> 406<p>The following illustrates a hypothetical example of a REPL that performs 407translation of text from one language to another:</p> 408<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>); 409<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>); 410 411<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>); 412 413<span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span class="hljs-params">cmd, context, filename, callback</span>) { 414 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, myTranslator.<span class="hljs-title function_">translate</span>(cmd)); 415} 416 417repl.<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> 418<h5>Recoverable errors<span><a class="mark" href="#repl_recoverable_errors" id="repl_recoverable_errors">#</a></span></h5> 419<p>At the REPL prompt, pressing <kbd>Enter</kbd> sends the current line of input to 420the <code>eval</code> function. In order to support multi-line input, the <code>eval</code> function 421can return an instance of <code>repl.Recoverable</code> to the provided callback function:</p> 422<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>) { 423 <span class="hljs-keyword">let</span> result; 424 <span class="hljs-keyword">try</span> { 425 result = vm.<span class="hljs-title function_">runInThisContext</span>(cmd); 426 } <span class="hljs-keyword">catch</span> (e) { 427 <span class="hljs-keyword">if</span> (<span class="hljs-title function_">isRecoverableError</span>(e)) { 428 <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)); 429 } 430 } 431 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, result); 432} 433 434<span class="hljs-keyword">function</span> <span class="hljs-title function_">isRecoverableError</span>(<span class="hljs-params">error</span>) { 435 <span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span class="hljs-string">'SyntaxError'</span>) { 436 <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>); 437 } 438 <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; 439}</code></pre> 440<h4>Customizing REPL output<span><a class="mark" href="#repl_customizing_repl_output" id="repl_customizing_repl_output">#</a></span></h4> 441<p>By default, <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> instances format output using the 442<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> 443stream (<code>process.stdout</code> by default). The <code>showProxy</code> inspection option is set 444to true by default and the <code>colors</code> option is set to true depending on the 445REPL's <code>useColors</code> option.</p> 446<p>The <code>useColors</code> boolean option can be specified at construction to instruct the 447default writer to use ANSI style codes to colorize the output from the 448<code>util.inspect()</code> method.</p> 449<p>If the REPL is run as standalone program, it is also possible to change the 450REPL's <a href="util.html#util_util_inspect_object_options">inspection defaults</a> from inside the REPL by using the 451<code>inspect.replDefaults</code> property which mirrors the <code>defaultOptions</code> from 452<a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a>.</p> 453<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> 454false 455<span class="hljs-meta">> </span><span class="language-bash">[1]</span> 456[ 457 1 458] 459<span class="hljs-meta">></span></code></pre> 460<p>To fully customize the output of a <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> instance pass in a new 461function for the <code>writer</code> option on construction. The following example, for 462instance, simply converts any input text to upper case:</p> 463<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>); 464 465<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 }); 466 467<span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span class="hljs-params">cmd, context, filename, callback</span>) { 468 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, cmd); 469} 470 471<span class="hljs-keyword">function</span> <span class="hljs-title function_">myWriter</span>(<span class="hljs-params">output</span>) { 472 <span class="hljs-keyword">return</span> output.<span class="hljs-title function_">toUpperCase</span>(); 473}</code></pre> 474</section><section><h3>Class: <code>REPLServer</code><span><a class="mark" href="#repl_class_replserver" id="repl_class_replserver">#</a></span></h3> 475<div class="api_metadata"> 476<span>Added in: v0.1.91</span> 477</div> 478<ul> 479<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#repl_repl_start_options"><code>repl.start()</code></a></li> 480<li>Extends: <a href="readline.html#readline_class_interface" class="type"><readline.Interface></a></li> 481</ul> 482<p>Instances of <code>repl.REPLServer</code> are created using the <a href="#repl_repl_start_options"><code>repl.start()</code></a> method 483or directly using the JavaScript <code>new</code> keyword.</p> 484<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>); 485 486<span class="hljs-keyword">const</span> options = { <span class="hljs-attr">useColors</span>: <span class="hljs-literal">true</span> }; 487 488<span class="hljs-keyword">const</span> firstInstance = repl.<span class="hljs-title function_">start</span>(options); 489<span class="hljs-keyword">const</span> secondInstance = <span class="hljs-keyword">new</span> repl.<span class="hljs-title function_">REPLServer</span>(options);</code></pre> 490<h4>Event: <code>'exit'</code><span><a class="mark" href="#repl_event_exit" id="repl_event_exit">#</a></span></h4> 491<div class="api_metadata"> 492<span>Added in: v0.7.7</span> 493</div> 494<p>The <code>'exit'</code> event is emitted when the REPL is exited either by receiving the 495<code>.exit</code> command as input, the user pressing <kbd>Ctrl</kbd>+<kbd>C</kbd> twice 496to signal <code>SIGINT</code>, 497or by pressing <kbd>Ctrl</kbd>+<kbd>D</kbd> to signal <code>'end'</code> on the input 498stream. The listener 499callback is invoked without any arguments.</p> 500<pre><code class="language-js">replServer.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> { 501 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Received "exit" event from repl!'</span>); 502 process.<span class="hljs-title function_">exit</span>(); 503});</code></pre> 504<h4>Event: <code>'reset'</code><span><a class="mark" href="#repl_event_reset" id="repl_event_reset">#</a></span></h4> 505<div class="api_metadata"> 506<span>Added in: v0.11.0</span> 507</div> 508<p>The <code>'reset'</code> event is emitted when the REPL's context is reset. This occurs 509whenever the <code>.clear</code> command is received as input <em>unless</em> the REPL is using 510the default evaluator and the <code>repl.REPLServer</code> instance was created with the 511<code>useGlobal</code> option set to <code>true</code>. The listener callback will be called with a 512reference to the <code>context</code> object as the only argument.</p> 513<p>This can be used primarily to re-initialize REPL context to some pre-defined 514state:</p> 515<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>); 516 517<span class="hljs-keyword">function</span> <span class="hljs-title function_">initializeContext</span>(<span class="hljs-params">context</span>) { 518 context.<span class="hljs-property">m</span> = <span class="hljs-string">'test'</span>; 519} 520 521<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> }); 522<span class="hljs-title function_">initializeContext</span>(r.<span class="hljs-property">context</span>); 523 524r.<span class="hljs-title function_">on</span>(<span class="hljs-string">'reset'</span>, initializeContext);</code></pre> 525<p>When this code is executed, the global <code>'m'</code> variable can be modified but then 526reset to its initial value using the <code>.clear</code> command:</p> 527<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">./node example.js</span> 528<span class="hljs-meta">> </span><span class="language-bash">m</span> 529'test' 530<span class="hljs-meta">> </span><span class="language-bash">m = 1</span> 5311 532<span class="hljs-meta">> </span><span class="language-bash">m</span> 5331 534<span class="hljs-meta">> </span><span class="language-bash">.clear</span> 535Clearing context... 536<span class="hljs-meta">> </span><span class="language-bash">m</span> 537'test' 538<span class="hljs-meta">></span></code></pre> 539<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> 540<div class="api_metadata"> 541<span>Added in: v0.3.0</span> 542</div> 543<ul> 544<li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The command keyword (<em>without</em> a leading <code>.</code> character).</li> 545<li><code>cmd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function to invoke when the command is processed.</li> 546</ul> 547<p>The <code>replServer.defineCommand()</code> method is used to add new <code>.</code>-prefixed commands 548to the REPL instance. Such commands are invoked by typing a <code>.</code> followed by the 549<code>keyword</code>. The <code>cmd</code> is either a <code>Function</code> or an <code>Object</code> with the following 550properties:</p> 551<ul> 552<li><code>help</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Help text to be displayed when <code>.help</code> is entered (Optional).</li> 553<li><code>action</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function to execute, optionally accepting a single 554string argument.</li> 555</ul> 556<p>The following example shows two new commands added to the REPL instance:</p> 557<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>); 558 559<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> }); 560replServer.<span class="hljs-title function_">defineCommand</span>(<span class="hljs-string">'sayhello'</span>, { 561 <span class="hljs-attr">help</span>: <span class="hljs-string">'Say hello'</span>, 562 <span class="hljs-title function_">action</span>(<span class="hljs-params">name</span>) { 563 <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">clearBufferedCommand</span>(); 564 <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>); 565 <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">displayPrompt</span>(); 566 } 567}); 568replServer.<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>) { 569 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Goodbye!'</span>); 570 <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">close</span>(); 571});</code></pre> 572<p>The new commands can then be used from within the REPL instance:</p> 573<pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">.sayhello Node.js User</span> 574Hello, Node.js User! 575<span class="hljs-meta">> </span><span class="language-bash">.saybye</span> 576Goodbye!</code></pre> 577<h4><code>replServer.displayPrompt([preserveCursor])</code><span><a class="mark" href="#repl_replserver_displayprompt_preservecursor" id="repl_replserver_displayprompt_preservecursor">#</a></span></h4> 578<div class="api_metadata"> 579<span>Added in: v0.1.91</span> 580</div> 581<ul> 582<li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 583</ul> 584<p>The <code>replServer.displayPrompt()</code> method readies the REPL instance for input 585from the user, printing the configured <code>prompt</code> to a new line in the <code>output</code> 586and resuming the <code>input</code> to accept new input.</p> 587<p>When multi-line input is being entered, an ellipsis is printed rather than the 588'prompt'.</p> 589<p>When <code>preserveCursor</code> is <code>true</code>, the cursor placement will not be reset to <code>0</code>.</p> 590<p>The <code>replServer.displayPrompt</code> method is primarily intended to be called from 591within the action function for commands registered using the 592<code>replServer.defineCommand()</code> method.</p> 593<h4><code>replServer.clearBufferedCommand()</code><span><a class="mark" href="#repl_replserver_clearbufferedcommand" id="repl_replserver_clearbufferedcommand">#</a></span></h4> 594<div class="api_metadata"> 595<span>Added in: v9.0.0</span> 596</div> 597<p>The <code>replServer.clearBufferedCommand()</code> method clears any command that has been 598buffered but not yet executed. This method is primarily intended to be 599called from within the action function for commands registered using the 600<code>replServer.defineCommand()</code> method.</p> 601<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> 602<div class="api_metadata"> 603<span>Added in: v0.8.9</span><span>Deprecated since: v9.0.0</span> 604</div> 605<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated.</div><p></p> 606<ul> 607<li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> the potential keyword to parse and execute</li> 608<li><code>rest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> any parameters to the keyword command</li> 609<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 610</ul> 611<p>An internal method used to parse and execute <code>REPLServer</code> keywords. 612Returns <code>true</code> if <code>keyword</code> is a valid keyword, otherwise <code>false</code>.</p> 613<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> 614<div class="api_metadata"> 615<span>Added in: v11.10.0</span> 616</div> 617<ul> 618<li><code>historyPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> the path to the history file</li> 619<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> called when history writes are ready or upon error 620<ul> 621<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 622<li><code>repl</code> <a href="repl.html#repl_class_replserver" class="type"><repl.REPLServer></a></li> 623</ul> 624</li> 625</ul> 626<p>Initializes a history log file for the REPL instance. When executing the 627Node.js binary and using the command-line REPL, a history file is initialized 628by default. However, this is not the case when creating a REPL 629programmatically. Use this method to initialize a history log file when working 630with REPL instances programmatically.</p> 631</section><section><h3><code>repl.builtinModules</code><span><a class="mark" href="#repl_repl_builtinmodules" id="repl_repl_builtinmodules">#</a></span></h3> 632<div class="api_metadata"> 633<span>Added in: v14.5.0</span> 634</div> 635<ul> 636<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 637</ul> 638<p>A list of the names of all Node.js modules, e.g., <code>'http'</code>.</p> 639</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> 640<div class="api_metadata"> 641<details class="changelog"><summary>History</summary> 642<table> 643<tbody><tr><th>Version</th><th>Changes</th></tr> 644<tr><td>v13.4.0</td> 645<td><p>The <code>preview</code> option is now available.</p></td></tr> 646<tr><td>v12.0.0</td> 647<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> 648<tr><td>v10.0.0</td> 649<td><p>The <code>REPL_MAGIC_MODE</code> <code>replMode</code> was removed.</p></td></tr> 650<tr><td>v6.3.0</td> 651<td><p>The <code>breakEvalOnSigint</code> option is supported now.</p></td></tr> 652<tr><td>v5.8.0</td> 653<td><p>The <code>options</code> parameter is optional now.</p></td></tr> 654<tr><td>v0.1.91</td> 655<td><p><span>Added in: v0.1.91</span></p></td></tr> 656</tbody></table> 657</details> 658</div> 659<ul> 660<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 661<ul> 662<li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The input prompt to display. <strong>Default:</strong> <code>'> '</code> 663(with a trailing space).</li> 664<li><code>input</code> <a href="stream.html#stream_class_stream_readable" class="type"><stream.Readable></a> The <code>Readable</code> stream from which REPL input will 665be read. <strong>Default:</strong> <code>process.stdin</code>.</li> 666<li><code>output</code> <a href="stream.html#stream_class_stream_writable" class="type"><stream.Writable></a> The <code>Writable</code> stream to which REPL output will 667be written. <strong>Default:</strong> <code>process.stdout</code>.</li> 668<li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, specifies that the <code>output</code> should be 669treated as a TTY terminal. 670<strong>Default:</strong> checking the value of the <code>isTTY</code> property on the <code>output</code> 671stream upon instantiation.</li> 672<li><code>eval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function to be used when evaluating each given line 673of input. <strong>Default:</strong> an async wrapper for the JavaScript <code>eval()</code> 674function. An <code>eval</code> function can error with <code>repl.Recoverable</code> to indicate 675the input was incomplete and prompt for additional lines.</li> 676<li><code>useColors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, specifies that the default <code>writer</code> 677function should include ANSI color styling to REPL output. If a custom 678<code>writer</code> function is provided then this has no effect. <strong>Default:</strong> checking 679color support on the <code>output</code> stream if the REPL instance's <code>terminal</code> value 680is <code>true</code>.</li> 681<li><code>useGlobal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, specifies that the default evaluation 682function will use the JavaScript <code>global</code> as the context as opposed to 683creating a new separate context for the REPL instance. The node CLI REPL 684sets this value to <code>true</code>. <strong>Default:</strong> <code>false</code>.</li> 685<li><code>ignoreUndefined</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, specifies that the default writer 686will not output the return value of a command if it evaluates to 687<code>undefined</code>. <strong>Default:</strong> <code>false</code>.</li> 688<li><code>writer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function to invoke to format the output of each 689command before writing to <code>output</code>. <strong>Default:</strong> <a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a>.</li> 690<li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> An optional function used for custom Tab auto 691completion. See <a href="readline.html#readline_use_of_the_completer_function"><code>readline.InterfaceCompleter</code></a> for an example.</li> 692<li><code>replMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> A flag that specifies whether the default evaluator 693executes all JavaScript commands in strict mode or default (sloppy) mode. 694Acceptable values are: 695<ul> 696<li><code>repl.REPL_MODE_SLOPPY</code> to evaluate expressions in sloppy mode.</li> 697<li><code>repl.REPL_MODE_STRICT</code> to evaluate expressions in strict mode. This is 698equivalent to prefacing every repl statement with <code>'use strict'</code>.</li> 699</ul> 700</li> 701<li><code>breakEvalOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Stop evaluating the current piece of code when 702<code>SIGINT</code> is received, such as when <kbd>Ctrl</kbd>+<kbd>C</kbd> is pressed. 703This cannot be used 704together with a custom <code>eval</code> function. <strong>Default:</strong> <code>false</code>.</li> 705<li><code>preview</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Defines if the repl prints autocomplete and output 706previews or not. <strong>Default:</strong> <code>true</code> with the default eval function and 707<code>false</code> in case a custom eval function is used. If <code>terminal</code> is falsy, then 708there are no previews and the value of <code>preview</code> has no effect.</li> 709</ul> 710</li> 711<li>Returns: <a href="repl.html#repl_class_replserver" class="type"><repl.REPLServer></a></li> 712</ul> 713<p>The <code>repl.start()</code> method creates and starts a <a href="#repl_class_replserver"><code>repl.REPLServer</code></a> instance.</p> 714<p>If <code>options</code> is a string, then it specifies the input prompt:</p> 715<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>); 716 717<span class="hljs-comment">// a Unix style prompt</span> 718repl.<span class="hljs-title function_">start</span>(<span class="hljs-string">'$ '</span>);</code></pre> 719</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> 720<p>Node.js itself uses the <code>repl</code> module to provide its own interactive interface 721for executing JavaScript. This can be used by executing the Node.js binary 722without passing any arguments (or by passing the <code>-i</code> argument):</p> 723<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node</span> 724<span class="hljs-meta">> </span><span class="language-bash">const a = [1, 2, 3];</span> 725undefined 726<span class="hljs-meta">> </span><span class="language-bash">a</span> 727[ 1, 2, 3 ] 728<span class="hljs-meta">> </span><span class="language-bash">a.forEach((v) => {</span> 729... console.log(v); 730... }); 7311 7322 7333</code></pre> 734<h4>Environment variable options<span><a class="mark" href="#repl_environment_variable_options" id="repl_environment_variable_options">#</a></span></h4> 735<p>Various behaviors of the Node.js REPL can be customized using the following 736environment variables:</p> 737<ul> 738<li><code>NODE_REPL_HISTORY</code>: When a valid path is given, persistent REPL history 739will be saved to the specified file rather than <code>.node_repl_history</code> in the 740user's home directory. Setting this value to <code>''</code> (an empty string) will 741disable persistent REPL history. Whitespace will be trimmed from the value. 742On Windows platforms environment variables with empty values are invalid so 743set this variable to one or more spaces to disable persistent REPL history.</li> 744<li><code>NODE_REPL_HISTORY_SIZE</code>: Controls how many lines of history will be 745persisted if history is available. Must be a positive number. 746<strong>Default:</strong> <code>1000</code>.</li> 747<li><code>NODE_REPL_MODE</code>: May be either <code>'sloppy'</code> or <code>'strict'</code>. <strong>Default:</strong> 748<code>'sloppy'</code>, which will allow non-strict mode code to be run.</li> 749</ul> 750<h4>Persistent history<span><a class="mark" href="#repl_persistent_history" id="repl_persistent_history">#</a></span></h4> 751<p>By default, the Node.js REPL will persist history between <code>node</code> REPL sessions 752by saving inputs to a <code>.node_repl_history</code> file located in the user's home 753directory. This can be disabled by setting the environment variable 754<code>NODE_REPL_HISTORY=''</code>.</p> 755<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> 756<p>For advanced line-editors, start Node.js with the environment variable 757<code>NODE_NO_READLINE=1</code>. This will start the main and debugger REPL in canonical 758terminal settings, which will allow use with <code>rlwrap</code>.</p> 759<p>For example, the following can be added to a <code>.bashrc</code> file:</p> 760<pre><code class="language-text">alias node="env NODE_NO_READLINE=1 rlwrap node"</code></pre> 761<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> 762<p>It is possible to create and run multiple REPL instances against a single 763running instance of Node.js that share a single <code>global</code> object but have 764separate I/O interfaces.</p> 765<p>The following example, for instance, provides separate REPLs on <code>stdin</code>, a Unix 766socket, and a TCP socket:</p> 767<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>); 768<span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'repl'</span>); 769<span class="hljs-keyword">let</span> connections = <span class="hljs-number">0</span>; 770 771repl.<span class="hljs-title function_">start</span>({ 772 <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via stdin> '</span>, 773 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>, 774 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span> 775}); 776 777net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> { 778 connections += <span class="hljs-number">1</span>; 779 repl.<span class="hljs-title function_">start</span>({ 780 <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via Unix socket> '</span>, 781 <span class="hljs-attr">input</span>: socket, 782 <span class="hljs-attr">output</span>: socket 783 }).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> { 784 socket.<span class="hljs-title function_">end</span>(); 785 }); 786}).<span class="hljs-title function_">listen</span>(<span class="hljs-string">'/tmp/node-repl-sock'</span>); 787 788net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> { 789 connections += <span class="hljs-number">1</span>; 790 repl.<span class="hljs-title function_">start</span>({ 791 <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via TCP socket> '</span>, 792 <span class="hljs-attr">input</span>: socket, 793 <span class="hljs-attr">output</span>: socket 794 }).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> { 795 socket.<span class="hljs-title function_">end</span>(); 796 }); 797}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">5001</span>);</code></pre> 798<p>Running this application from the command line will start a REPL on stdin. 799Other REPL clients may connect through the Unix socket or TCP socket. <code>telnet</code>, 800for instance, is useful for connecting to TCP sockets, while <code>socat</code> can be used 801to connect to both Unix and TCP sockets.</p> 802<p>By starting a REPL from a Unix socket-based server instead of stdin, it is 803possible to connect to a long-running Node.js process without restarting it.</p> 804<p>For an example of running a "full-featured" (<code>terminal</code>) REPL over 805a <code>net.Server</code> and <code>net.Socket</code> instance, see: 806<a href="https://gist.github.com/TooTallNate/2209310">https://gist.github.com/TooTallNate/2209310</a>.</p> 807<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: 808<a href="https://gist.github.com/TooTallNate/2053342">https://gist.github.com/TooTallNate/2053342</a>.</p></section> 809 <!-- API END --> 810 </div> 811 </div> 812 </div> 813 <script> 814 'use strict'; 815 { 816 const kCustomPreference = 'customDarkTheme'; 817 const userSettings = sessionStorage.getItem(kCustomPreference); 818 const themeToggleButton = document.getElementById('theme-toggle-btn'); 819 if (userSettings === null && window.matchMedia) { 820 const mq = window.matchMedia('(prefers-color-scheme: dark)'); 821 if ('onchange' in mq) { 822 function mqChangeListener(e) { 823 document.body.classList.toggle('dark-mode', e.matches); 824 } 825 mq.addEventListener('change', mqChangeListener); 826 if (themeToggleButton) { 827 themeToggleButton.addEventListener('click', function() { 828 mq.removeEventListener('change', mqChangeListener); 829 }, { once: true }); 830 } 831 } 832 if (mq.matches) { 833 document.body.classList.add('dark-mode'); 834 } 835 } else if (userSettings === 'true') { 836 document.body.classList.add('dark-mode'); 837 } 838 if (themeToggleButton) { 839 themeToggleButton.hidden = false; 840 themeToggleButton.addEventListener('click', function() { 841 sessionStorage.setItem( 842 kCustomPreference, 843 document.body.classList.toggle('dark-mode') 844 ); 845 }); 846 } 847 } 848 </script> 849</body> 850</html> 851