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