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