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.18.2"> 7 <title>Readline | Node.js v18.18.2 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/readline.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 <style>@media(max-width:630px){.with-51-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:558px){.with-42-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:742px){.with-65-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 14</head> 15<body class="alt apidoc" id="api-section-readline"> 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 active">Readline</a></li> 69<li><a href="repl.html" class="nav-repl">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="readline" class="interior"> 97 <header class="header"> 98 <div class="header-container"> 99 <h1>Node.js v18.18.2 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.18.2</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="#readline">Readline</a></span> 124<ul> 125<li><a href="#class-interfaceconstructor">Class: <code>InterfaceConstructor</code></a> 126<ul> 127<li><a href="#event-close">Event: <code>'close'</code></a></li> 128<li><a href="#event-line">Event: <code>'line'</code></a></li> 129<li><a href="#event-history">Event: <code>'history'</code></a></li> 130<li><a href="#event-pause">Event: <code>'pause'</code></a></li> 131<li><a href="#event-resume">Event: <code>'resume'</code></a></li> 132<li><a href="#event-sigcont">Event: <code>'SIGCONT'</code></a></li> 133<li><a href="#event-sigint">Event: <code>'SIGINT'</code></a></li> 134<li><a href="#event-sigtstp">Event: <code>'SIGTSTP'</code></a></li> 135<li><a href="#rlclose"><code>rl.close()</code></a></li> 136<li><a href="#rlpause"><code>rl.pause()</code></a></li> 137<li><a href="#rlpromptpreservecursor"><code>rl.prompt([preserveCursor])</code></a></li> 138<li><a href="#rlquestionquery-options-callback"><code>rl.question(query[, options], callback)</code></a></li> 139<li><a href="#rlresume"><code>rl.resume()</code></a></li> 140<li><a href="#rlsetpromptprompt"><code>rl.setPrompt(prompt)</code></a></li> 141<li><a href="#rlgetprompt"><code>rl.getPrompt()</code></a></li> 142<li><a href="#rlwritedata-key"><code>rl.write(data[, key])</code></a></li> 143<li><a href="#rlsymbolasynciterator"><code>rl[Symbol.asyncIterator]()</code></a></li> 144<li><a href="#rlline"><code>rl.line</code></a></li> 145<li><a href="#rlcursor"><code>rl.cursor</code></a></li> 146<li><a href="#rlgetcursorpos"><code>rl.getCursorPos()</code></a></li> 147</ul> 148</li> 149<li><span class="stability_1"><a href="#promises-api">Promises API</a></span> 150<ul> 151<li><a href="#class-readlinepromisesinterface">Class: <code>readlinePromises.Interface</code></a> 152<ul> 153<li><a href="#rlquestionquery-options"><code>rl.question(query[, options])</code></a></li> 154</ul> 155</li> 156<li><a href="#class-readlinepromisesreadline">Class: <code>readlinePromises.Readline</code></a> 157<ul> 158<li><a href="#new-readlinepromisesreadlinestream-options"><code>new readlinePromises.Readline(stream[, options])</code></a></li> 159<li><a href="#rlclearlinedir"><code>rl.clearLine(dir)</code></a></li> 160<li><a href="#rlclearscreendown"><code>rl.clearScreenDown()</code></a></li> 161<li><a href="#rlcommit"><code>rl.commit()</code></a></li> 162<li><a href="#rlcursortox-y"><code>rl.cursorTo(x[, y])</code></a></li> 163<li><a href="#rlmovecursordx-dy"><code>rl.moveCursor(dx, dy)</code></a></li> 164<li><a href="#rlrollback"><code>rl.rollback()</code></a></li> 165</ul> 166</li> 167<li><a href="#readlinepromisescreateinterfaceoptions"><code>readlinePromises.createInterface(options)</code></a> 168<ul> 169<li><a href="#use-of-the-completer-function">Use of the <code>completer</code> function</a></li> 170</ul> 171</li> 172</ul> 173</li> 174<li><a href="#callback-api">Callback API</a> 175<ul> 176<li><a href="#class-readlineinterface">Class: <code>readline.Interface</code></a> 177<ul> 178<li><a href="#rlquestionquery-options-callback_1"><code>rl.question(query[, options], callback)</code></a></li> 179</ul> 180</li> 181<li><a href="#readlineclearlinestream-dir-callback"><code>readline.clearLine(stream, dir[, callback])</code></a></li> 182<li><a href="#readlineclearscreendownstream-callback"><code>readline.clearScreenDown(stream[, callback])</code></a></li> 183<li><a href="#readlinecreateinterfaceoptions"><code>readline.createInterface(options)</code></a> 184<ul> 185<li><a href="#use-of-the-completer-function_1">Use of the <code>completer</code> function</a></li> 186</ul> 187</li> 188<li><a href="#readlinecursortostream-x-y-callback"><code>readline.cursorTo(stream, x[, y][, callback])</code></a></li> 189<li><a href="#readlinemovecursorstream-dx-dy-callback"><code>readline.moveCursor(stream, dx, dy[, callback])</code></a></li> 190</ul> 191</li> 192<li><a href="#readlineemitkeypresseventsstream-interface"><code>readline.emitKeypressEvents(stream[, interface])</code></a></li> 193<li><a href="#example-tiny-cli">Example: Tiny CLI</a></li> 194<li><a href="#example-read-file-stream-line-by-line">Example: Read file stream line-by-Line</a></li> 195<li><a href="#tty-keybindings">TTY keybindings</a></li> 196</ul> 197</li> 198</ul></div></div> 199 </li> 200 201 202 <li class="picker-header"> 203 <a href="#"> 204 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 205 Index 206 </a> 207 208 <div class="picker"><ul> 209<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 210<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 211 212 <li> 213 <a href="index.html">Index</a> 214 </li> 215 </ul> 216 217<hr class="line"> 218<ul> 219<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 220<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 221<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 222<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 223<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 224<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 225<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 226<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 227<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 228<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 229<li><a href="console.html" class="nav-console">Console</a></li> 230<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 231<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 232<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 233<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 234<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 235<li><a href="dns.html" class="nav-dns">DNS</a></li> 236<li><a href="domain.html" class="nav-domain">Domain</a></li> 237<li><a href="errors.html" class="nav-errors">Errors</a></li> 238<li><a href="events.html" class="nav-events">Events</a></li> 239<li><a href="fs.html" class="nav-fs">File system</a></li> 240<li><a href="globals.html" class="nav-globals">Globals</a></li> 241<li><a href="http.html" class="nav-http">HTTP</a></li> 242<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 243<li><a href="https.html" class="nav-https">HTTPS</a></li> 244<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 245<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 246<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 247<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 248<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 249<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 250<li><a href="net.html" class="nav-net">Net</a></li> 251<li><a href="os.html" class="nav-os">OS</a></li> 252<li><a href="path.html" class="nav-path">Path</a></li> 253<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 254<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 255<li><a href="process.html" class="nav-process">Process</a></li> 256<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 257<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 258<li><a href="readline.html" class="nav-readline active">Readline</a></li> 259<li><a href="repl.html" class="nav-repl">REPL</a></li> 260<li><a href="report.html" class="nav-report">Report</a></li> 261<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 262<li><a href="stream.html" class="nav-stream">Stream</a></li> 263<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 264<li><a href="test.html" class="nav-test">Test runner</a></li> 265<li><a href="timers.html" class="nav-timers">Timers</a></li> 266<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 267<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 268<li><a href="tty.html" class="nav-tty">TTY</a></li> 269<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 270<li><a href="url.html" class="nav-url">URL</a></li> 271<li><a href="util.html" class="nav-util">Utilities</a></li> 272<li><a href="v8.html" class="nav-v8">V8</a></li> 273<li><a href="vm.html" class="nav-vm">VM</a></li> 274<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 275<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 276<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 277<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 278<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 279</ul> 280<hr class="line"> 281<ul> 282<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 283</ul></div> 284 </li> 285 286 287 <li class="picker-header"> 288 <a href="#"> 289 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 290 Other versions 291 </a> 292 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/readline.html">20.x</a></li> 293<li><a href="https://nodejs.org/docs/latest-v19.x/api/readline.html">19.x</a></li> 294<li><a href="https://nodejs.org/docs/latest-v18.x/api/readline.html">18.x <b>LTS</b></a></li> 295<li><a href="https://nodejs.org/docs/latest-v17.x/api/readline.html">17.x</a></li> 296<li><a href="https://nodejs.org/docs/latest-v16.x/api/readline.html">16.x</a></li> 297<li><a href="https://nodejs.org/docs/latest-v15.x/api/readline.html">15.x</a></li> 298<li><a href="https://nodejs.org/docs/latest-v14.x/api/readline.html">14.x</a></li> 299<li><a href="https://nodejs.org/docs/latest-v13.x/api/readline.html">13.x</a></li> 300<li><a href="https://nodejs.org/docs/latest-v12.x/api/readline.html">12.x</a></li> 301<li><a href="https://nodejs.org/docs/latest-v11.x/api/readline.html">11.x</a></li> 302<li><a href="https://nodejs.org/docs/latest-v10.x/api/readline.html">10.x</a></li> 303<li><a href="https://nodejs.org/docs/latest-v9.x/api/readline.html">9.x</a></li> 304<li><a href="https://nodejs.org/docs/latest-v8.x/api/readline.html">8.x</a></li> 305<li><a href="https://nodejs.org/docs/latest-v7.x/api/readline.html">7.x</a></li> 306<li><a href="https://nodejs.org/docs/latest-v6.x/api/readline.html">6.x</a></li> 307<li><a href="https://nodejs.org/docs/latest-v5.x/api/readline.html">5.x</a></li> 308<li><a href="https://nodejs.org/docs/latest-v4.x/api/readline.html">4.x</a></li> 309<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/readline.html">0.12.x</a></li> 310<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/readline.html">0.10.x</a></li></ol></div> 311 </li> 312 313 <li class="picker-header"> 314 <a href="#"> 315 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 316 Options 317 </a> 318 319 <div class="picker"> 320 <ul> 321 <li> 322 <a href="all.html">View on single page</a> 323 </li> 324 <li> 325 <a href="readline.json">View as JSON</a> 326 </li> 327 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/readline.md">Edit on GitHub</a></li> 328 </ul> 329 </div> 330 </li> 331 </ul> 332 </div> 333 <hr> 334 </header> 335 336 <details id="toc" open><summary>Table of contents</summary><ul> 337<li><span class="stability_2"><a href="#readline">Readline</a></span> 338<ul> 339<li><a href="#class-interfaceconstructor">Class: <code>InterfaceConstructor</code></a> 340<ul> 341<li><a href="#event-close">Event: <code>'close'</code></a></li> 342<li><a href="#event-line">Event: <code>'line'</code></a></li> 343<li><a href="#event-history">Event: <code>'history'</code></a></li> 344<li><a href="#event-pause">Event: <code>'pause'</code></a></li> 345<li><a href="#event-resume">Event: <code>'resume'</code></a></li> 346<li><a href="#event-sigcont">Event: <code>'SIGCONT'</code></a></li> 347<li><a href="#event-sigint">Event: <code>'SIGINT'</code></a></li> 348<li><a href="#event-sigtstp">Event: <code>'SIGTSTP'</code></a></li> 349<li><a href="#rlclose"><code>rl.close()</code></a></li> 350<li><a href="#rlpause"><code>rl.pause()</code></a></li> 351<li><a href="#rlpromptpreservecursor"><code>rl.prompt([preserveCursor])</code></a></li> 352<li><a href="#rlquestionquery-options-callback"><code>rl.question(query[, options], callback)</code></a></li> 353<li><a href="#rlresume"><code>rl.resume()</code></a></li> 354<li><a href="#rlsetpromptprompt"><code>rl.setPrompt(prompt)</code></a></li> 355<li><a href="#rlgetprompt"><code>rl.getPrompt()</code></a></li> 356<li><a href="#rlwritedata-key"><code>rl.write(data[, key])</code></a></li> 357<li><a href="#rlsymbolasynciterator"><code>rl[Symbol.asyncIterator]()</code></a></li> 358<li><a href="#rlline"><code>rl.line</code></a></li> 359<li><a href="#rlcursor"><code>rl.cursor</code></a></li> 360<li><a href="#rlgetcursorpos"><code>rl.getCursorPos()</code></a></li> 361</ul> 362</li> 363<li><span class="stability_1"><a href="#promises-api">Promises API</a></span> 364<ul> 365<li><a href="#class-readlinepromisesinterface">Class: <code>readlinePromises.Interface</code></a> 366<ul> 367<li><a href="#rlquestionquery-options"><code>rl.question(query[, options])</code></a></li> 368</ul> 369</li> 370<li><a href="#class-readlinepromisesreadline">Class: <code>readlinePromises.Readline</code></a> 371<ul> 372<li><a href="#new-readlinepromisesreadlinestream-options"><code>new readlinePromises.Readline(stream[, options])</code></a></li> 373<li><a href="#rlclearlinedir"><code>rl.clearLine(dir)</code></a></li> 374<li><a href="#rlclearscreendown"><code>rl.clearScreenDown()</code></a></li> 375<li><a href="#rlcommit"><code>rl.commit()</code></a></li> 376<li><a href="#rlcursortox-y"><code>rl.cursorTo(x[, y])</code></a></li> 377<li><a href="#rlmovecursordx-dy"><code>rl.moveCursor(dx, dy)</code></a></li> 378<li><a href="#rlrollback"><code>rl.rollback()</code></a></li> 379</ul> 380</li> 381<li><a href="#readlinepromisescreateinterfaceoptions"><code>readlinePromises.createInterface(options)</code></a> 382<ul> 383<li><a href="#use-of-the-completer-function">Use of the <code>completer</code> function</a></li> 384</ul> 385</li> 386</ul> 387</li> 388<li><a href="#callback-api">Callback API</a> 389<ul> 390<li><a href="#class-readlineinterface">Class: <code>readline.Interface</code></a> 391<ul> 392<li><a href="#rlquestionquery-options-callback_1"><code>rl.question(query[, options], callback)</code></a></li> 393</ul> 394</li> 395<li><a href="#readlineclearlinestream-dir-callback"><code>readline.clearLine(stream, dir[, callback])</code></a></li> 396<li><a href="#readlineclearscreendownstream-callback"><code>readline.clearScreenDown(stream[, callback])</code></a></li> 397<li><a href="#readlinecreateinterfaceoptions"><code>readline.createInterface(options)</code></a> 398<ul> 399<li><a href="#use-of-the-completer-function_1">Use of the <code>completer</code> function</a></li> 400</ul> 401</li> 402<li><a href="#readlinecursortostream-x-y-callback"><code>readline.cursorTo(stream, x[, y][, callback])</code></a></li> 403<li><a href="#readlinemovecursorstream-dx-dy-callback"><code>readline.moveCursor(stream, dx, dy[, callback])</code></a></li> 404</ul> 405</li> 406<li><a href="#readlineemitkeypresseventsstream-interface"><code>readline.emitKeypressEvents(stream[, interface])</code></a></li> 407<li><a href="#example-tiny-cli">Example: Tiny CLI</a></li> 408<li><a href="#example-read-file-stream-line-by-line">Example: Read file stream line-by-Line</a></li> 409<li><a href="#tty-keybindings">TTY keybindings</a></li> 410</ul> 411</li> 412</ul></details> 413 414 <div id="apicontent"> 415 <h2>Readline<span><a class="mark" href="#readline" id="readline">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline"></a></h2> 416 417<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 418<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/readline.js">lib/readline.js</a></p> 419<p>The <code>node:readline</code> module provides an interface for reading data from a 420<a href="stream.html#readable-streams">Readable</a> stream (such as <a href="process.html#processstdin"><code>process.stdin</code></a>) one line at a time.</p> 421<p>To use the promise-based APIs:</p> 422 423<pre class="with-51-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> readline <span class="hljs-keyword">from</span> <span class="hljs-string">'node:readline/promises'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline/promises'</span>);</code><button class="copy-button">copy</button></pre> 424<p>To use the callback and sync APIs:</p> 425 426<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> readline <span class="hljs-keyword">from</span> <span class="hljs-string">'node:readline'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>);</code><button class="copy-button">copy</button></pre> 427<p>The following simple example illustrates the basic use of the <code>node:readline</code> 428module.</p> 429 430<pre class="with-65-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> readline <span class="hljs-keyword">from</span> <span class="hljs-string">'node:readline/promises'</span>; 431<span class="hljs-keyword">import</span> { stdin <span class="hljs-keyword">as</span> input, stdout <span class="hljs-keyword">as</span> output } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 432 433<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ input, output }); 434 435<span class="hljs-keyword">const</span> answer = <span class="hljs-keyword">await</span> rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What do you think of Node.js? '</span>); 436 437<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Thank you for your valuable feedback: <span class="hljs-subst">${answer}</span>`</span>); 438 439rl.<span class="hljs-title function_">close</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>); 440<span class="hljs-keyword">const</span> { <span class="hljs-attr">stdin</span>: input, <span class="hljs-attr">stdout</span>: output } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>); 441 442<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ input, output }); 443 444rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What do you think of Node.js? '</span>, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> { 445 <span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> Log the answer in a database</span> 446 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Thank you for your valuable feedback: <span class="hljs-subst">${answer}</span>`</span>); 447 448 rl.<span class="hljs-title function_">close</span>(); 449});</code><button class="copy-button">copy</button></pre> 450<p>Once this code is invoked, the Node.js application will not terminate until the 451<code>readline.Interface</code> is closed because the interface waits for data to be 452received on the <code>input</code> stream.</p> 453<p><a id="readline_class_interface"></a></p> 454<section><h3>Class: <code>InterfaceConstructor</code><span><a class="mark" href="#class-interfaceconstructor" id="class-interfaceconstructor">#</a></span><a aria-hidden="true" class="legacy" id="readline_class_interfaceconstructor"></a></h3> 455<div class="api_metadata"> 456<span>Added in: v0.1.104</span> 457</div> 458<ul> 459<li>Extends: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 460</ul> 461<p>Instances of the <code>InterfaceConstructor</code> class are constructed using the 462<code>readlinePromises.createInterface()</code> or <code>readline.createInterface()</code> method. 463Every instance is associated with a single <code>input</code> <a href="stream.html#readable-streams">Readable</a> stream and a 464single <code>output</code> <a href="stream.html#writable-streams">Writable</a> stream. 465The <code>output</code> stream is used to print prompts for user input that arrives on, 466and is read from, the <code>input</code> stream.</p> 467<h4>Event: <code>'close'</code><span><a class="mark" href="#event-close" id="event-close">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_close"></a></h4> 468<div class="api_metadata"> 469<span>Added in: v0.1.98</span> 470</div> 471<p>The <code>'close'</code> event is emitted when one of the following occur:</p> 472<ul> 473<li>The <code>rl.close()</code> method is called and the <code>InterfaceConstructor</code> instance has 474relinquished control over the <code>input</code> and <code>output</code> streams;</li> 475<li>The <code>input</code> stream receives its <code>'end'</code> event;</li> 476<li>The <code>input</code> stream receives <kbd>Ctrl</kbd>+<kbd>D</kbd> to signal 477end-of-transmission (EOT);</li> 478<li>The <code>input</code> stream receives <kbd>Ctrl</kbd>+<kbd>C</kbd> to signal <code>SIGINT</code> 479and there is no <code>'SIGINT'</code> event listener registered on the 480<code>InterfaceConstructor</code> instance.</li> 481</ul> 482<p>The listener function is called without passing any arguments.</p> 483<p>The <code>InterfaceConstructor</code> instance is finished once the <code>'close'</code> event is 484emitted.</p> 485<h4>Event: <code>'line'</code><span><a class="mark" href="#event-line" id="event-line">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_line"></a></h4> 486<div class="api_metadata"> 487<span>Added in: v0.1.98</span> 488</div> 489<p>The <code>'line'</code> event is emitted whenever the <code>input</code> stream receives an 490end-of-line input (<code>\n</code>, <code>\r</code>, or <code>\r\n</code>). This usually occurs when the user 491presses <kbd>Enter</kbd> or <kbd>Return</kbd>.</p> 492<p>The <code>'line'</code> event is also emitted if new data has been read from a stream and 493that stream ends without a final end-of-line marker.</p> 494<p>The listener function is called with a string containing the single line of 495received input.</p> 496<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">input</span>) =></span> { 497 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Received: <span class="hljs-subst">${input}</span>`</span>); 498});</code> <button class="copy-button">copy</button></pre> 499<h4>Event: <code>'history'</code><span><a class="mark" href="#event-history" id="event-history">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_history"></a></h4> 500<div class="api_metadata"> 501<span>Added in: v15.8.0, v14.18.0</span> 502</div> 503<p>The <code>'history'</code> event is emitted whenever the history array has changed.</p> 504<p>The listener function is called with an array containing the history array. 505It will reflect all changes, added lines and removed lines due to 506<code>historySize</code> and <code>removeHistoryDuplicates</code>.</p> 507<p>The primary purpose is to allow a listener to persist the history. 508It is also possible for the listener to change the history object. This 509could be useful to prevent certain lines to be added to the history, like 510a password.</p> 511<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'history'</span>, <span class="hljs-function">(<span class="hljs-params">history</span>) =></span> { 512 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Received: <span class="hljs-subst">${history}</span>`</span>); 513});</code> <button class="copy-button">copy</button></pre> 514<h4>Event: <code>'pause'</code><span><a class="mark" href="#event-pause" id="event-pause">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_pause"></a></h4> 515<div class="api_metadata"> 516<span>Added in: v0.7.5</span> 517</div> 518<p>The <code>'pause'</code> event is emitted when one of the following occur:</p> 519<ul> 520<li>The <code>input</code> stream is paused.</li> 521<li>The <code>input</code> stream is not paused and receives the <code>'SIGCONT'</code> event. (See 522events <a href="#event-sigtstp"><code>'SIGTSTP'</code></a> and <a href="#event-sigcont"><code>'SIGCONT'</code></a>.)</li> 523</ul> 524<p>The listener function is called without passing any arguments.</p> 525<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'pause'</span>, <span class="hljs-function">() =></span> { 526 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Readline paused.'</span>); 527});</code> <button class="copy-button">copy</button></pre> 528<h4>Event: <code>'resume'</code><span><a class="mark" href="#event-resume" id="event-resume">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_resume"></a></h4> 529<div class="api_metadata"> 530<span>Added in: v0.7.5</span> 531</div> 532<p>The <code>'resume'</code> event is emitted whenever the <code>input</code> stream is resumed.</p> 533<p>The listener function is called without passing any arguments.</p> 534<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'resume'</span>, <span class="hljs-function">() =></span> { 535 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Readline resumed.'</span>); 536});</code> <button class="copy-button">copy</button></pre> 537<h4>Event: <code>'SIGCONT'</code><span><a class="mark" href="#event-sigcont" id="event-sigcont">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_sigcont"></a></h4> 538<div class="api_metadata"> 539<span>Added in: v0.7.5</span> 540</div> 541<p>The <code>'SIGCONT'</code> event is emitted when a Node.js process previously moved into 542the background using <kbd>Ctrl</kbd>+<kbd>Z</kbd> (i.e. <code>SIGTSTP</code>) is then 543brought back to the foreground using <a href="http://man7.org/linux/man-pages/man1/fg.1p.html"><code>fg(1p)</code></a>.</p> 544<p>If the <code>input</code> stream was paused <em>before</em> the <code>SIGTSTP</code> request, this event will 545not be emitted.</p> 546<p>The listener function is invoked without passing any arguments.</p> 547<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGCONT'</span>, <span class="hljs-function">() =></span> { 548 <span class="hljs-comment">// `prompt` will automatically resume the stream</span> 549 rl.<span class="hljs-title function_">prompt</span>(); 550});</code> <button class="copy-button">copy</button></pre> 551<p>The <code>'SIGCONT'</code> event is <em>not</em> supported on Windows.</p> 552<h4>Event: <code>'SIGINT'</code><span><a class="mark" href="#event-sigint" id="event-sigint">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_sigint"></a></h4> 553<div class="api_metadata"> 554<span>Added in: v0.3.0</span> 555</div> 556<p>The <code>'SIGINT'</code> event is emitted whenever the <code>input</code> stream receives 557a <kbd>Ctrl+C</kbd> input, known typically as <code>SIGINT</code>. If there are no 558<code>'SIGINT'</code> event listeners registered when the <code>input</code> stream receives a 559<code>SIGINT</code>, the <code>'pause'</code> event will be emitted.</p> 560<p>The listener function is invoked without passing any arguments.</p> 561<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGINT'</span>, <span class="hljs-function">() =></span> { 562 rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'Are you sure you want to exit? '</span>, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> { 563 <span class="hljs-keyword">if</span> (answer.<span class="hljs-title function_">match</span>(<span class="hljs-regexp">/^y(es)?$/i</span>)) rl.<span class="hljs-title function_">pause</span>(); 564 }); 565});</code> <button class="copy-button">copy</button></pre> 566<h4>Event: <code>'SIGTSTP'</code><span><a class="mark" href="#event-sigtstp" id="event-sigtstp">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_sigtstp"></a></h4> 567<div class="api_metadata"> 568<span>Added in: v0.7.5</span> 569</div> 570<p>The <code>'SIGTSTP'</code> event is emitted when the <code>input</code> stream receives 571a <kbd>Ctrl</kbd>+<kbd>Z</kbd> input, typically known as <code>SIGTSTP</code>. If there are 572no <code>'SIGTSTP'</code> event listeners registered when the <code>input</code> stream receives a 573<code>SIGTSTP</code>, the Node.js process will be sent to the background.</p> 574<p>When the program is resumed using <a href="http://man7.org/linux/man-pages/man1/fg.1p.html"><code>fg(1p)</code></a>, the <code>'pause'</code> and <code>'SIGCONT'</code> events 575will be emitted. These can be used to resume the <code>input</code> stream.</p> 576<p>The <code>'pause'</code> and <code>'SIGCONT'</code> events will not be emitted if the <code>input</code> was 577paused before the process was sent to the background.</p> 578<p>The listener function is invoked without passing any arguments.</p> 579<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGTSTP'</span>, <span class="hljs-function">() =></span> { 580 <span class="hljs-comment">// This will override SIGTSTP and prevent the program from going to the</span> 581 <span class="hljs-comment">// background.</span> 582 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Caught SIGTSTP.'</span>); 583});</code> <button class="copy-button">copy</button></pre> 584<p>The <code>'SIGTSTP'</code> event is <em>not</em> supported on Windows.</p> 585<h4><code>rl.close()</code><span><a class="mark" href="#rlclose" id="rlclose">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_close"></a></h4> 586<div class="api_metadata"> 587<span>Added in: v0.1.98</span> 588</div> 589<p>The <code>rl.close()</code> method closes the <code>InterfaceConstructor</code> instance and 590relinquishes control over the <code>input</code> and <code>output</code> streams. When called, 591the <code>'close'</code> event will be emitted.</p> 592<p>Calling <code>rl.close()</code> does not immediately stop other events (including <code>'line'</code>) 593from being emitted by the <code>InterfaceConstructor</code> instance.</p> 594<h4><code>rl.pause()</code><span><a class="mark" href="#rlpause" id="rlpause">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_pause"></a></h4> 595<div class="api_metadata"> 596<span>Added in: v0.3.4</span> 597</div> 598<p>The <code>rl.pause()</code> method pauses the <code>input</code> stream, allowing it to be resumed 599later if necessary.</p> 600<p>Calling <code>rl.pause()</code> does not immediately pause other events (including 601<code>'line'</code>) from being emitted by the <code>InterfaceConstructor</code> instance.</p> 602<h4><code>rl.prompt([preserveCursor])</code><span><a class="mark" href="#rlpromptpreservecursor" id="rlpromptpreservecursor">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_prompt_preservecursor"></a></h4> 603<div class="api_metadata"> 604<span>Added in: v0.1.98</span> 605</div> 606<ul> 607<li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, prevents the cursor placement from 608being reset to <code>0</code>.</li> 609</ul> 610<p>The <code>rl.prompt()</code> method writes the <code>InterfaceConstructor</code> instances configured 611<code>prompt</code> to a new line in <code>output</code> in order to provide a user with a new 612location at which to provide input.</p> 613<p>When called, <code>rl.prompt()</code> will resume the <code>input</code> stream if it has been 614paused.</p> 615<p>If the <code>InterfaceConstructor</code> was created with <code>output</code> set to <code>null</code> or 616<code>undefined</code> the prompt is not written.</p> 617<h4><code>rl.question(query[, options], callback)</code><span><a class="mark" href="#rlquestionquery-options-callback" id="rlquestionquery-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_question_query_options_callback"></a></h4> 618<div class="api_metadata"> 619<span>Added in: v0.3.3</span> 620</div> 621<ul> 622<li><code>query</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A statement or query to write to <code>output</code>, prepended to the 623prompt.</li> 624<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 625<ul> 626<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Optionally allows the <code>question()</code> to be canceled 627using an <code>AbortController</code>.</li> 628</ul> 629</li> 630<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A callback function that is invoked with the user's 631input in response to the <code>query</code>.</li> 632</ul> 633<p>The <code>rl.question()</code> method displays the <code>query</code> by writing it to the <code>output</code>, 634waits for user input to be provided on <code>input</code>, then invokes the <code>callback</code> 635function passing the provided input as the first argument.</p> 636<p>When called, <code>rl.question()</code> will resume the <code>input</code> stream if it has been 637paused.</p> 638<p>If the <code>InterfaceConstructor</code> was created with <code>output</code> set to <code>null</code> or 639<code>undefined</code> the <code>query</code> is not written.</p> 640<p>The <code>callback</code> function passed to <code>rl.question()</code> does not follow the typical 641pattern of accepting an <code>Error</code> object or <code>null</code> as the first argument. 642The <code>callback</code> is called with the provided answer as the only argument.</p> 643<p>An error will be thrown if calling <code>rl.question()</code> after <code>rl.close()</code>.</p> 644<p>Example usage:</p> 645<pre><code class="language-js">rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> { 646 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Oh, so your favorite food is <span class="hljs-subst">${answer}</span>`</span>); 647});</code> <button class="copy-button">copy</button></pre> 648<p>Using an <code>AbortController</code> to cancel a question.</p> 649<pre><code class="language-js"><span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 650<span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>; 651 652rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>, { signal }, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> { 653 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Oh, so your favorite food is <span class="hljs-subst">${answer}</span>`</span>); 654}); 655 656signal.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'abort'</span>, <span class="hljs-function">() =></span> { 657 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The food question timed out'</span>); 658}, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> }); 659 660<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-title function_">abort</span>(), <span class="hljs-number">10000</span>);</code> <button class="copy-button">copy</button></pre> 661<h4><code>rl.resume()</code><span><a class="mark" href="#rlresume" id="rlresume">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_resume"></a></h4> 662<div class="api_metadata"> 663<span>Added in: v0.3.4</span> 664</div> 665<p>The <code>rl.resume()</code> method resumes the <code>input</code> stream if it has been paused.</p> 666<h4><code>rl.setPrompt(prompt)</code><span><a class="mark" href="#rlsetpromptprompt" id="rlsetpromptprompt">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_setprompt_prompt"></a></h4> 667<div class="api_metadata"> 668<span>Added in: v0.1.98</span> 669</div> 670<ul> 671<li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 672</ul> 673<p>The <code>rl.setPrompt()</code> method sets the prompt that will be written to <code>output</code> 674whenever <code>rl.prompt()</code> is called.</p> 675<h4><code>rl.getPrompt()</code><span><a class="mark" href="#rlgetprompt" id="rlgetprompt">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_getprompt"></a></h4> 676<div class="api_metadata"> 677<span>Added in: v15.3.0, v14.17.0</span> 678</div> 679<ul> 680<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> the current prompt string</li> 681</ul> 682<p>The <code>rl.getPrompt()</code> method returns the current prompt used by <code>rl.prompt()</code>.</p> 683<h4><code>rl.write(data[, key])</code><span><a class="mark" href="#rlwritedata-key" id="rlwritedata-key">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_write_data_key"></a></h4> 684<div class="api_metadata"> 685<span>Added in: v0.1.98</span> 686</div> 687<ul> 688<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 689<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 690<ul> 691<li><code>ctrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> to indicate the <kbd>Ctrl</kbd> key.</li> 692<li><code>meta</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> to indicate the <kbd>Meta</kbd> key.</li> 693<li><code>shift</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> to indicate the <kbd>Shift</kbd> key.</li> 694<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name of the a key.</li> 695</ul> 696</li> 697</ul> 698<p>The <code>rl.write()</code> method will write either <code>data</code> or a key sequence identified 699by <code>key</code> to the <code>output</code>. The <code>key</code> argument is supported only if <code>output</code> is 700a <a href="tty.html">TTY</a> text terminal. See <a href="#tty-keybindings">TTY keybindings</a> for a list of key 701combinations.</p> 702<p>If <code>key</code> is specified, <code>data</code> is ignored.</p> 703<p>When called, <code>rl.write()</code> will resume the <code>input</code> stream if it has been 704paused.</p> 705<p>If the <code>InterfaceConstructor</code> was created with <code>output</code> set to <code>null</code> or 706<code>undefined</code> the <code>data</code> and <code>key</code> are not written.</p> 707<pre><code class="language-js">rl.<span class="hljs-title function_">write</span>(<span class="hljs-string">'Delete this!'</span>); 708<span class="hljs-comment">// Simulate Ctrl+U to delete the line written previously</span> 709rl.<span class="hljs-title function_">write</span>(<span class="hljs-literal">null</span>, { <span class="hljs-attr">ctrl</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">name</span>: <span class="hljs-string">'u'</span> });</code> <button class="copy-button">copy</button></pre> 710<p>The <code>rl.write()</code> method will write the data to the <code>readline</code> <code>Interface</code>'s 711<code>input</code> <em>as if it were provided by the user</em>.</p> 712<h4><code>rl[Symbol.asyncIterator]()</code><span><a class="mark" href="#rlsymbolasynciterator" id="rlsymbolasynciterator">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_symbol_asynciterator"></a></h4> 713<div class="api_metadata"> 714<details class="changelog"><summary>History</summary> 715<table> 716<tbody><tr><th>Version</th><th>Changes</th></tr> 717<tr><td>v11.14.0, v10.17.0</td> 718<td><p>Symbol.asyncIterator support is no longer experimental.</p></td></tr> 719<tr><td>v11.4.0, v10.16.0</td> 720<td><p><span>Added in: v11.4.0, v10.16.0</span></p></td></tr> 721</tbody></table> 722</details> 723</div> 724<ul> 725<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a></li> 726</ul> 727<p>Create an <code>AsyncIterator</code> object that iterates through each line in the input 728stream as a string. This method allows asynchronous iteration of 729<code>InterfaceConstructor</code> objects through <code>for await...of</code> loops.</p> 730<p>Errors in the input stream are not forwarded.</p> 731<p>If the loop is terminated with <code>break</code>, <code>throw</code>, or <code>return</code>, 732<a href="#rlclose"><code>rl.close()</code></a> will be called. In other words, iterating over a 733<code>InterfaceConstructor</code> will always consume the input stream fully.</p> 734<p>Performance is not on par with the traditional <code>'line'</code> event API. Use <code>'line'</code> 735instead for performance-sensitive applications.</p> 736<pre><code class="language-js"><span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">processLineByLine</span>(<span class="hljs-params"></span>) { 737 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ 738 <span class="hljs-comment">// ...</span> 739 }); 740 741 <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> line <span class="hljs-keyword">of</span> rl) { 742 <span class="hljs-comment">// Each line in the readline input will be successively available here as</span> 743 <span class="hljs-comment">// `line`.</span> 744 } 745}</code> <button class="copy-button">copy</button></pre> 746<p><code>readline.createInterface()</code> will start to consume the input stream once 747invoked. Having asynchronous operations between interface creation and 748asynchronous iteration may result in missed lines.</p> 749<h4><code>rl.line</code><span><a class="mark" href="#rlline" id="rlline">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_line"></a></h4> 750<div class="api_metadata"> 751<details class="changelog"><summary>History</summary> 752<table> 753<tbody><tr><th>Version</th><th>Changes</th></tr> 754<tr><td>v15.8.0, v14.18.0</td> 755<td><p>Value will always be a string, never undefined.</p></td></tr> 756<tr><td>v0.1.98</td> 757<td><p><span>Added in: v0.1.98</span></p></td></tr> 758</tbody></table> 759</details> 760</div> 761<ul> 762<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 763</ul> 764<p>The current input data being processed by node.</p> 765<p>This can be used when collecting input from a TTY stream to retrieve the 766current value that has been processed thus far, prior to the <code>line</code> event 767being emitted. Once the <code>line</code> event has been emitted, this property will 768be an empty string.</p> 769<p>Be aware that modifying the value during the instance runtime may have 770unintended consequences if <code>rl.cursor</code> is not also controlled.</p> 771<p><strong>If not using a TTY stream for input, use the <a href="#event-line"><code>'line'</code></a> event.</strong></p> 772<p>One possible use case would be as follows:</p> 773<pre><code class="language-js"><span class="hljs-keyword">const</span> values = [<span class="hljs-string">'lorem ipsum'</span>, <span class="hljs-string">'dolor sit amet'</span>]; 774<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>(process.<span class="hljs-property">stdin</span>); 775<span class="hljs-keyword">const</span> showResults = <span class="hljs-title function_">debounce</span>(<span class="hljs-function">() =></span> { 776 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( 777 <span class="hljs-string">'\n'</span>, 778 values.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">val</span>) =></span> val.<span class="hljs-title function_">startsWith</span>(rl.<span class="hljs-property">line</span>)).<span class="hljs-title function_">join</span>(<span class="hljs-string">' '</span>), 779 ); 780}, <span class="hljs-number">300</span>); 781process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'keypress'</span>, <span class="hljs-function">(<span class="hljs-params">c, k</span>) =></span> { 782 <span class="hljs-title function_">showResults</span>(); 783});</code> <button class="copy-button">copy</button></pre> 784<h4><code>rl.cursor</code><span><a class="mark" href="#rlcursor" id="rlcursor">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_cursor"></a></h4> 785<div class="api_metadata"> 786<span>Added in: v0.1.98</span> 787</div> 788<ul> 789<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li> 790</ul> 791<p>The cursor position relative to <code>rl.line</code>.</p> 792<p>This will track where the current cursor lands in the input string, when 793reading input from a TTY stream. The position of cursor determines the 794portion of the input string that will be modified as input is processed, 795as well as the column where the terminal caret will be rendered.</p> 796<h4><code>rl.getCursorPos()</code><span><a class="mark" href="#rlgetcursorpos" id="rlgetcursorpos">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_getcursorpos"></a></h4> 797<div class="api_metadata"> 798<span>Added in: v13.5.0, v12.16.0</span> 799</div> 800<ul> 801<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 802<ul> 803<li><code>rows</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> the row of the prompt the cursor currently lands on</li> 804<li><code>cols</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> the screen column the cursor currently lands on</li> 805</ul> 806</li> 807</ul> 808<p>Returns the real position of the cursor in relation to the input 809prompt + string. Long input (wrapping) strings, as well as multiple 810line prompts are included in the calculations.</p> 811</section><section><h3>Promises API<span><a class="mark" href="#promises-api" id="promises-api">#</a></span><a aria-hidden="true" class="legacy" id="readline_promises_api"></a></h3> 812<div class="api_metadata"> 813<span>Added in: v17.0.0</span> 814</div> 815<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 816<h4>Class: <code>readlinePromises.Interface</code><span><a class="mark" href="#class-readlinepromisesinterface" id="class-readlinepromisesinterface">#</a></span><a aria-hidden="true" class="legacy" id="readline_class_readlinepromises_interface"></a></h4> 817<div class="api_metadata"> 818<span>Added in: v17.0.0</span> 819</div> 820<ul> 821<li>Extends: <a href="readline.html#class-interfaceconstructor" class="type"><readline.InterfaceConstructor></a></li> 822</ul> 823<p>Instances of the <code>readlinePromises.Interface</code> class are constructed using the 824<code>readlinePromises.createInterface()</code> method. Every instance is associated with a 825single <code>input</code> <a href="stream.html#readable-streams">Readable</a> stream and a single <code>output</code> <a href="stream.html#writable-streams">Writable</a> stream. 826The <code>output</code> stream is used to print prompts for user input that arrives on, 827and is read from, the <code>input</code> stream.</p> 828<h5><code>rl.question(query[, options])</code><span><a class="mark" href="#rlquestionquery-options" id="rlquestionquery-options">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_question_query_options"></a></h5> 829<div class="api_metadata"> 830<span>Added in: v17.0.0</span> 831</div> 832<ul> 833<li><code>query</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A statement or query to write to <code>output</code>, prepended to the 834prompt.</li> 835<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 836<ul> 837<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Optionally allows the <code>question()</code> to be canceled 838using an <code>AbortSignal</code>.</li> 839</ul> 840</li> 841<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> A promise that is fulfilled with the user's 842input in response to the <code>query</code>.</li> 843</ul> 844<p>The <code>rl.question()</code> method displays the <code>query</code> by writing it to the <code>output</code>, 845waits for user input to be provided on <code>input</code>, then invokes the <code>callback</code> 846function passing the provided input as the first argument.</p> 847<p>When called, <code>rl.question()</code> will resume the <code>input</code> stream if it has been 848paused.</p> 849<p>If the <code>readlinePromises.Interface</code> was created with <code>output</code> set to <code>null</code> or 850<code>undefined</code> the <code>query</code> is not written.</p> 851<p>If the question is called after <code>rl.close()</code>, it returns a rejected promise.</p> 852<p>Example usage:</p> 853<pre><code class="language-js mjs"><span class="hljs-keyword">const</span> answer = <span class="hljs-keyword">await</span> rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>); 854<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Oh, so your favorite food is <span class="hljs-subst">${answer}</span>`</span>);</code> <button class="copy-button">copy</button></pre> 855<p>Using an <code>AbortSignal</code> to cancel a question.</p> 856<pre><code class="language-js mjs"><span class="hljs-keyword">const</span> signal = <span class="hljs-title class_">AbortSignal</span>.<span class="hljs-title function_">timeout</span>(<span class="hljs-number">10_000</span>); 857 858signal.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'abort'</span>, <span class="hljs-function">() =></span> { 859 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The food question timed out'</span>); 860}, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> }); 861 862<span class="hljs-keyword">const</span> answer = <span class="hljs-keyword">await</span> rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>, { signal }); 863<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Oh, so your favorite food is <span class="hljs-subst">${answer}</span>`</span>);</code> <button class="copy-button">copy</button></pre> 864<h4>Class: <code>readlinePromises.Readline</code><span><a class="mark" href="#class-readlinepromisesreadline" id="class-readlinepromisesreadline">#</a></span><a aria-hidden="true" class="legacy" id="readline_class_readlinepromises_readline"></a></h4> 865<div class="api_metadata"> 866<span>Added in: v17.0.0</span> 867</div> 868<h5><code>new readlinePromises.Readline(stream[, options])</code><span><a class="mark" href="#new-readlinepromisesreadlinestream-options" id="new-readlinepromisesreadlinestream-options">#</a></span><a aria-hidden="true" class="legacy" id="readline_new_readlinepromises_readline_stream_options"></a></h5> 869<div class="api_metadata"> 870<span>Added in: v17.0.0</span> 871</div> 872<ul> 873<li><code>stream</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a> A <a href="tty.html">TTY</a> stream.</li> 874<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 875<ul> 876<li><code>autoCommit</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, no need to call <code>rl.commit()</code>.</li> 877</ul> 878</li> 879</ul> 880<h5><code>rl.clearLine(dir)</code><span><a class="mark" href="#rlclearlinedir" id="rlclearlinedir">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_clearline_dir"></a></h5> 881<div class="api_metadata"> 882<span>Added in: v17.0.0</span> 883</div> 884<ul> 885<li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> 886<ul> 887<li><code>-1</code>: to the left from cursor</li> 888<li><code>1</code>: to the right from cursor</li> 889<li><code>0</code>: the entire line</li> 890</ul> 891</li> 892<li>Returns: this</li> 893</ul> 894<p>The <code>rl.clearLine()</code> method adds to the internal list of pending action an 895action that clears current line of the associated <code>stream</code> in a specified 896direction identified by <code>dir</code>. 897Call <code>rl.commit()</code> to see the effect of this method, unless <code>autoCommit: true</code> 898was passed to the constructor.</p> 899<h5><code>rl.clearScreenDown()</code><span><a class="mark" href="#rlclearscreendown" id="rlclearscreendown">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_clearscreendown"></a></h5> 900<div class="api_metadata"> 901<span>Added in: v17.0.0</span> 902</div> 903<ul> 904<li>Returns: this</li> 905</ul> 906<p>The <code>rl.clearScreenDown()</code> method adds to the internal list of pending action an 907action that clears the associated stream from the current position of the 908cursor down. 909Call <code>rl.commit()</code> to see the effect of this method, unless <code>autoCommit: true</code> 910was passed to the constructor.</p> 911<h5><code>rl.commit()</code><span><a class="mark" href="#rlcommit" id="rlcommit">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_commit"></a></h5> 912<div class="api_metadata"> 913<span>Added in: v17.0.0</span> 914</div> 915<ul> 916<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 917</ul> 918<p>The <code>rl.commit()</code> method sends all the pending actions to the associated 919<code>stream</code> and clears the internal list of pending actions.</p> 920<h5><code>rl.cursorTo(x[, y])</code><span><a class="mark" href="#rlcursortox-y" id="rlcursortox-y">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_cursorto_x_y"></a></h5> 921<div class="api_metadata"> 922<span>Added in: v17.0.0</span> 923</div> 924<ul> 925<li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 926<li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 927<li>Returns: this</li> 928</ul> 929<p>The <code>rl.cursorTo()</code> method adds to the internal list of pending action an action 930that moves cursor to the specified position in the associated <code>stream</code>. 931Call <code>rl.commit()</code> to see the effect of this method, unless <code>autoCommit: true</code> 932was passed to the constructor.</p> 933<h5><code>rl.moveCursor(dx, dy)</code><span><a class="mark" href="#rlmovecursordx-dy" id="rlmovecursordx-dy">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_movecursor_dx_dy"></a></h5> 934<div class="api_metadata"> 935<span>Added in: v17.0.0</span> 936</div> 937<ul> 938<li><code>dx</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 939<li><code>dy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 940<li>Returns: this</li> 941</ul> 942<p>The <code>rl.moveCursor()</code> method adds to the internal list of pending action an 943action that moves the cursor <em>relative</em> to its current position in the 944associated <code>stream</code>. 945Call <code>rl.commit()</code> to see the effect of this method, unless <code>autoCommit: true</code> 946was passed to the constructor.</p> 947<h5><code>rl.rollback()</code><span><a class="mark" href="#rlrollback" id="rlrollback">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_rollback"></a></h5> 948<div class="api_metadata"> 949<span>Added in: v17.0.0</span> 950</div> 951<ul> 952<li>Returns: this</li> 953</ul> 954<p>The <code>rl.rollback</code> methods clears the internal list of pending actions without 955sending it to the associated <code>stream</code>.</p> 956<h4><code>readlinePromises.createInterface(options)</code><span><a class="mark" href="#readlinepromisescreateinterfaceoptions" id="readlinepromisescreateinterfaceoptions">#</a></span><a aria-hidden="true" class="legacy" id="readline_readlinepromises_createinterface_options"></a></h4> 957<div class="api_metadata"> 958<span>Added in: v17.0.0</span> 959</div> 960<ul> 961<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 962<ul> 963<li><code>input</code> <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> The <a href="stream.html#readable-streams">Readable</a> stream to listen to. This option 964is <em>required</em>.</li> 965<li><code>output</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a> The <a href="stream.html#writable-streams">Writable</a> stream to write readline data 966to.</li> 967<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 Tab autocompletion.</li> 968<li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the <code>input</code> and <code>output</code> streams should be 969treated like a TTY, and have ANSI/VT100 escape codes written to it. 970<strong>Default:</strong> checking <code>isTTY</code> on the <code>output</code> stream upon instantiation.</li> 971<li><code>history</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> Initial list of history lines. This option makes sense 972only if <code>terminal</code> is set to <code>true</code> by the user or by an internal <code>output</code> 973check, otherwise the history caching mechanism is not initialized at all. 974<strong>Default:</strong> <code>[]</code>.</li> 975<li><code>historySize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Maximum number of history lines retained. To disable 976the history set this value to <code>0</code>. This option makes sense only if 977<code>terminal</code> is set to <code>true</code> by the user or by an internal <code>output</code> check, 978otherwise the history caching mechanism is not initialized at all. 979<strong>Default:</strong> <code>30</code>.</li> 980<li><code>removeHistoryDuplicates</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, when a new input line added 981to the history list duplicates an older one, this removes the older line 982from the list. <strong>Default:</strong> <code>false</code>.</li> 983<li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The prompt string to use. <strong>Default:</strong> <code>'> '</code>.</li> 984<li><code>crlfDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> If the delay between <code>\r</code> and <code>\n</code> exceeds 985<code>crlfDelay</code> milliseconds, both <code>\r</code> and <code>\n</code> will be treated as separate 986end-of-line input. <code>crlfDelay</code> will be coerced to a number no less than 987<code>100</code>. It can be set to <code>Infinity</code>, in which case <code>\r</code> followed by <code>\n</code> 988will always be considered a single newline (which may be reasonable for 989<a href="#example-read-file-stream-line-by-line">reading files</a> with <code>\r\n</code> line delimiter). <strong>Default:</strong> <code>100</code>.</li> 990<li><code>escapeCodeTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The duration <code>readlinePromises</code> will wait for a 991character (when reading an ambiguous key sequence in milliseconds one that 992can both form a complete key sequence using the input read so far and can 993take additional input to complete a longer key sequence). 994<strong>Default:</strong> <code>500</code>.</li> 995<li><code>tabSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of spaces a tab is equal to (minimum 1). 996<strong>Default:</strong> <code>8</code>.</li> 997</ul> 998</li> 999<li>Returns: <a href="readline.html#class-readlinepromisesinterface" class="type"><readlinePromises.Interface></a></li> 1000</ul> 1001<p>The <code>readlinePromises.createInterface()</code> method creates a new <code>readlinePromises.Interface</code> 1002instance.</p> 1003<pre><code class="language-js"><span class="hljs-keyword">const</span> readlinePromises = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline/promises'</span>); 1004<span class="hljs-keyword">const</span> rl = readlinePromises.<span class="hljs-title function_">createInterface</span>({ 1005 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>, 1006 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>, 1007});</code> <button class="copy-button">copy</button></pre> 1008<p>Once the <code>readlinePromises.Interface</code> instance is created, the most common case 1009is to listen for the <code>'line'</code> event:</p> 1010<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> { 1011 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Received: <span class="hljs-subst">${line}</span>`</span>); 1012});</code> <button class="copy-button">copy</button></pre> 1013<p>If <code>terminal</code> is <code>true</code> for this instance then the <code>output</code> stream will get 1014the best compatibility if it defines an <code>output.columns</code> property and emits 1015a <code>'resize'</code> event on the <code>output</code> if or when the columns ever change 1016(<a href="process.html#processstdout"><code>process.stdout</code></a> does this automatically when it is a TTY).</p> 1017<h5>Use of the <code>completer</code> function<span><a class="mark" href="#use-of-the-completer-function" id="use-of-the-completer-function">#</a></span><a aria-hidden="true" class="legacy" id="readline_use_of_the_completer_function"></a></h5> 1018<p>The <code>completer</code> function takes the current line entered by the user 1019as an argument, and returns an <code>Array</code> with 2 entries:</p> 1020<ul> 1021<li>An <code>Array</code> with matching entries for the completion.</li> 1022<li>The substring that was used for the matching.</li> 1023</ul> 1024<p>For instance: <code>[[substr1, substr2, ...], originalsubstring]</code>.</p> 1025<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">completer</span>(<span class="hljs-params">line</span>) { 1026 <span class="hljs-keyword">const</span> completions = <span class="hljs-string">'.help .error .exit .quit .q'</span>.<span class="hljs-title function_">split</span>(<span class="hljs-string">' '</span>); 1027 <span class="hljs-keyword">const</span> hits = completions.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">c</span>) =></span> c.<span class="hljs-title function_">startsWith</span>(line)); 1028 <span class="hljs-comment">// Show all completions if none found</span> 1029 <span class="hljs-keyword">return</span> [hits.<span class="hljs-property">length</span> ? hits : completions, line]; 1030}</code> <button class="copy-button">copy</button></pre> 1031<p>The <code>completer</code> function can also return a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a>, or be asynchronous:</p> 1032<pre><code class="language-js"><span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">completer</span>(<span class="hljs-params">linePartial</span>) { 1033 <span class="hljs-keyword">await</span> <span class="hljs-title function_">someAsyncWork</span>(); 1034 <span class="hljs-keyword">return</span> [[<span class="hljs-string">'123'</span>], linePartial]; 1035}</code> <button class="copy-button">copy</button></pre> 1036</section><section><h3>Callback API<span><a class="mark" href="#callback-api" id="callback-api">#</a></span><a aria-hidden="true" class="legacy" id="readline_callback_api"></a></h3> 1037<div class="api_metadata"> 1038<span>Added in: v0.1.104</span> 1039</div> 1040<h4>Class: <code>readline.Interface</code><span><a class="mark" href="#class-readlineinterface" id="class-readlineinterface">#</a></span><a aria-hidden="true" class="legacy" id="readline_class_readline_interface"></a></h4> 1041<div class="api_metadata"> 1042<details class="changelog"><summary>History</summary> 1043<table> 1044<tbody><tr><th>Version</th><th>Changes</th></tr> 1045<tr><td>v17.0.0</td> 1046<td><p>The class <code>readline.Interface</code> now inherits from <code>Interface</code>.</p></td></tr> 1047<tr><td>v0.1.104</td> 1048<td><p><span>Added in: v0.1.104</span></p></td></tr> 1049</tbody></table> 1050</details> 1051</div> 1052<ul> 1053<li>Extends: <a href="readline.html#class-interfaceconstructor" class="type"><readline.InterfaceConstructor></a></li> 1054</ul> 1055<p>Instances of the <code>readline.Interface</code> class are constructed using the 1056<code>readline.createInterface()</code> method. Every instance is associated with a 1057single <code>input</code> <a href="stream.html#readable-streams">Readable</a> stream and a single <code>output</code> <a href="stream.html#writable-streams">Writable</a> stream. 1058The <code>output</code> stream is used to print prompts for user input that arrives on, 1059and is read from, the <code>input</code> stream.</p> 1060<h5><code>rl.question(query[, options], callback)</code><span><a class="mark" href="#rlquestionquery-options-callback_1" id="rlquestionquery-options-callback_1">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_question_query_options_callback_1"></a></h5> 1061<div class="api_metadata"> 1062<span>Added in: v0.3.3</span> 1063</div> 1064<ul> 1065<li><code>query</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A statement or query to write to <code>output</code>, prepended to the 1066prompt.</li> 1067<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1068<ul> 1069<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Optionally allows the <code>question()</code> to be canceled 1070using an <code>AbortController</code>.</li> 1071</ul> 1072</li> 1073<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A callback function that is invoked with the user's 1074input in response to the <code>query</code>.</li> 1075</ul> 1076<p>The <code>rl.question()</code> method displays the <code>query</code> by writing it to the <code>output</code>, 1077waits for user input to be provided on <code>input</code>, then invokes the <code>callback</code> 1078function passing the provided input as the first argument.</p> 1079<p>When called, <code>rl.question()</code> will resume the <code>input</code> stream if it has been 1080paused.</p> 1081<p>If the <code>readline.Interface</code> was created with <code>output</code> set to <code>null</code> or 1082<code>undefined</code> the <code>query</code> is not written.</p> 1083<p>The <code>callback</code> function passed to <code>rl.question()</code> does not follow the typical 1084pattern of accepting an <code>Error</code> object or <code>null</code> as the first argument. 1085The <code>callback</code> is called with the provided answer as the only argument.</p> 1086<p>An error will be thrown if calling <code>rl.question()</code> after <code>rl.close()</code>.</p> 1087<p>Example usage:</p> 1088<pre><code class="language-js">rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> { 1089 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Oh, so your favorite food is <span class="hljs-subst">${answer}</span>`</span>); 1090});</code> <button class="copy-button">copy</button></pre> 1091<p>Using an <code>AbortController</code> to cancel a question.</p> 1092<pre><code class="language-js"><span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 1093<span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>; 1094 1095rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>, { signal }, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> { 1096 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Oh, so your favorite food is <span class="hljs-subst">${answer}</span>`</span>); 1097}); 1098 1099signal.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'abort'</span>, <span class="hljs-function">() =></span> { 1100 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The food question timed out'</span>); 1101}, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> }); 1102 1103<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-title function_">abort</span>(), <span class="hljs-number">10000</span>);</code> <button class="copy-button">copy</button></pre> 1104<h4><code>readline.clearLine(stream, dir[, callback])</code><span><a class="mark" href="#readlineclearlinestream-dir-callback" id="readlineclearlinestream-dir-callback">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline_clearline_stream_dir_callback"></a></h4> 1105<div class="api_metadata"> 1106<details class="changelog"><summary>History</summary> 1107<table> 1108<tbody><tr><th>Version</th><th>Changes</th></tr> 1109<tr><td>v18.0.0</td> 1110<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 1111<tr><td>v12.7.0</td> 1112<td><p>The stream's write() callback and return value are exposed.</p></td></tr> 1113<tr><td>v0.7.7</td> 1114<td><p><span>Added in: v0.7.7</span></p></td></tr> 1115</tbody></table> 1116</details> 1117</div> 1118<ul> 1119<li><code>stream</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 1120<li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> 1121<ul> 1122<li><code>-1</code>: to the left from cursor</li> 1123<li><code>1</code>: to the right from cursor</li> 1124<li><code>0</code>: the entire line</li> 1125</ul> 1126</li> 1127<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked once the operation completes.</li> 1128<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for 1129the <code>'drain'</code> event to be emitted before continuing to write additional data; 1130otherwise <code>true</code>.</li> 1131</ul> 1132<p>The <code>readline.clearLine()</code> method clears current line of given <a href="tty.html">TTY</a> stream 1133in a specified direction identified by <code>dir</code>.</p> 1134<h4><code>readline.clearScreenDown(stream[, callback])</code><span><a class="mark" href="#readlineclearscreendownstream-callback" id="readlineclearscreendownstream-callback">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline_clearscreendown_stream_callback"></a></h4> 1135<div class="api_metadata"> 1136<details class="changelog"><summary>History</summary> 1137<table> 1138<tbody><tr><th>Version</th><th>Changes</th></tr> 1139<tr><td>v18.0.0</td> 1140<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 1141<tr><td>v12.7.0</td> 1142<td><p>The stream's write() callback and return value are exposed.</p></td></tr> 1143<tr><td>v0.7.7</td> 1144<td><p><span>Added in: v0.7.7</span></p></td></tr> 1145</tbody></table> 1146</details> 1147</div> 1148<ul> 1149<li><code>stream</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 1150<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked once the operation completes.</li> 1151<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for 1152the <code>'drain'</code> event to be emitted before continuing to write additional data; 1153otherwise <code>true</code>.</li> 1154</ul> 1155<p>The <code>readline.clearScreenDown()</code> method clears the given <a href="tty.html">TTY</a> stream from 1156the current position of the cursor down.</p> 1157<h4><code>readline.createInterface(options)</code><span><a class="mark" href="#readlinecreateinterfaceoptions" id="readlinecreateinterfaceoptions">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline_createinterface_options"></a></h4> 1158<div class="api_metadata"> 1159<details class="changelog"><summary>History</summary> 1160<table> 1161<tbody><tr><th>Version</th><th>Changes</th></tr> 1162<tr><td>v15.14.0, v14.18.0</td> 1163<td><p>The <code>signal</code> option is supported now.</p></td></tr> 1164<tr><td>v15.8.0, v14.18.0</td> 1165<td><p>The <code>history</code> option is supported now.</p></td></tr> 1166<tr><td>v13.9.0</td> 1167<td><p>The <code>tabSize</code> option is supported now.</p></td></tr> 1168<tr><td>v8.3.0, v6.11.4</td> 1169<td><p>Remove max limit of <code>crlfDelay</code> option.</p></td></tr> 1170<tr><td>v6.6.0</td> 1171<td><p>The <code>crlfDelay</code> option is supported now.</p></td></tr> 1172<tr><td>v6.3.0</td> 1173<td><p>The <code>prompt</code> option is supported now.</p></td></tr> 1174<tr><td>v6.0.0</td> 1175<td><p>The <code>historySize</code> option can be <code>0</code> now.</p></td></tr> 1176<tr><td>v0.1.98</td> 1177<td><p><span>Added in: v0.1.98</span></p></td></tr> 1178</tbody></table> 1179</details> 1180</div> 1181<ul> 1182<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1183<ul> 1184<li><code>input</code> <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> The <a href="stream.html#readable-streams">Readable</a> stream to listen to. This option 1185is <em>required</em>.</li> 1186<li><code>output</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a> The <a href="stream.html#writable-streams">Writable</a> stream to write readline data 1187to.</li> 1188<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 Tab autocompletion.</li> 1189<li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the <code>input</code> and <code>output</code> streams should be 1190treated like a TTY, and have ANSI/VT100 escape codes written to it. 1191<strong>Default:</strong> checking <code>isTTY</code> on the <code>output</code> stream upon instantiation.</li> 1192<li><code>history</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> Initial list of history lines. This option makes sense 1193only if <code>terminal</code> is set to <code>true</code> by the user or by an internal <code>output</code> 1194check, otherwise the history caching mechanism is not initialized at all. 1195<strong>Default:</strong> <code>[]</code>.</li> 1196<li><code>historySize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Maximum number of history lines retained. To disable 1197the history set this value to <code>0</code>. This option makes sense only if 1198<code>terminal</code> is set to <code>true</code> by the user or by an internal <code>output</code> check, 1199otherwise the history caching mechanism is not initialized at all. 1200<strong>Default:</strong> <code>30</code>.</li> 1201<li><code>removeHistoryDuplicates</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, when a new input line added 1202to the history list duplicates an older one, this removes the older line 1203from the list. <strong>Default:</strong> <code>false</code>.</li> 1204<li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The prompt string to use. <strong>Default:</strong> <code>'> '</code>.</li> 1205<li><code>crlfDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> If the delay between <code>\r</code> and <code>\n</code> exceeds 1206<code>crlfDelay</code> milliseconds, both <code>\r</code> and <code>\n</code> will be treated as separate 1207end-of-line input. <code>crlfDelay</code> will be coerced to a number no less than 1208<code>100</code>. It can be set to <code>Infinity</code>, in which case <code>\r</code> followed by <code>\n</code> 1209will always be considered a single newline (which may be reasonable for 1210<a href="#example-read-file-stream-line-by-line">reading files</a> with <code>\r\n</code> line delimiter). <strong>Default:</strong> <code>100</code>.</li> 1211<li><code>escapeCodeTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The duration <code>readline</code> will wait for a 1212character (when reading an ambiguous key sequence in milliseconds one that 1213can both form a complete key sequence using the input read so far and can 1214take additional input to complete a longer key sequence). 1215<strong>Default:</strong> <code>500</code>.</li> 1216<li><code>tabSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of spaces a tab is equal to (minimum 1). 1217<strong>Default:</strong> <code>8</code>.</li> 1218<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows closing the interface using an AbortSignal. 1219Aborting the signal will internally call <code>close</code> on the interface.</li> 1220</ul> 1221</li> 1222<li>Returns: <a href="readline.html#class-readlineinterface" class="type"><readline.Interface></a></li> 1223</ul> 1224<p>The <code>readline.createInterface()</code> method creates a new <code>readline.Interface</code> 1225instance.</p> 1226<pre><code class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>); 1227<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ 1228 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>, 1229 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>, 1230});</code> <button class="copy-button">copy</button></pre> 1231<p>Once the <code>readline.Interface</code> instance is created, the most common case is to 1232listen for the <code>'line'</code> event:</p> 1233<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> { 1234 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Received: <span class="hljs-subst">${line}</span>`</span>); 1235});</code> <button class="copy-button">copy</button></pre> 1236<p>If <code>terminal</code> is <code>true</code> for this instance then the <code>output</code> stream will get 1237the best compatibility if it defines an <code>output.columns</code> property and emits 1238a <code>'resize'</code> event on the <code>output</code> if or when the columns ever change 1239(<a href="process.html#processstdout"><code>process.stdout</code></a> does this automatically when it is a TTY).</p> 1240<p>When creating a <code>readline.Interface</code> using <code>stdin</code> as input, the program 1241will not terminate until it receives an <a href="https://en.wikipedia.org/wiki/End-of-file#EOF_character">EOF character</a>. To exit without 1242waiting for user input, call <code>process.stdin.unref()</code>.</p> 1243<h5>Use of the <code>completer</code> function<span><a class="mark" href="#use-of-the-completer-function_1" id="use-of-the-completer-function_1">#</a></span><a aria-hidden="true" class="legacy" id="readline_use_of_the_completer_function_1"></a></h5> 1244<p>The <code>completer</code> function takes the current line entered by the user 1245as an argument, and returns an <code>Array</code> with 2 entries:</p> 1246<ul> 1247<li>An <code>Array</code> with matching entries for the completion.</li> 1248<li>The substring that was used for the matching.</li> 1249</ul> 1250<p>For instance: <code>[[substr1, substr2, ...], originalsubstring]</code>.</p> 1251<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">completer</span>(<span class="hljs-params">line</span>) { 1252 <span class="hljs-keyword">const</span> completions = <span class="hljs-string">'.help .error .exit .quit .q'</span>.<span class="hljs-title function_">split</span>(<span class="hljs-string">' '</span>); 1253 <span class="hljs-keyword">const</span> hits = completions.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">c</span>) =></span> c.<span class="hljs-title function_">startsWith</span>(line)); 1254 <span class="hljs-comment">// Show all completions if none found</span> 1255 <span class="hljs-keyword">return</span> [hits.<span class="hljs-property">length</span> ? hits : completions, line]; 1256}</code> <button class="copy-button">copy</button></pre> 1257<p>The <code>completer</code> function can be called asynchronously if it accepts two 1258arguments:</p> 1259<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">completer</span>(<span class="hljs-params">linePartial, callback</span>) { 1260 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, [[<span class="hljs-string">'123'</span>], linePartial]); 1261}</code> <button class="copy-button">copy</button></pre> 1262<h4><code>readline.cursorTo(stream, x[, y][, callback])</code><span><a class="mark" href="#readlinecursortostream-x-y-callback" id="readlinecursortostream-x-y-callback">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline_cursorto_stream_x_y_callback"></a></h4> 1263<div class="api_metadata"> 1264<details class="changelog"><summary>History</summary> 1265<table> 1266<tbody><tr><th>Version</th><th>Changes</th></tr> 1267<tr><td>v18.0.0</td> 1268<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 1269<tr><td>v12.7.0</td> 1270<td><p>The stream's write() callback and return value are exposed.</p></td></tr> 1271<tr><td>v0.7.7</td> 1272<td><p><span>Added in: v0.7.7</span></p></td></tr> 1273</tbody></table> 1274</details> 1275</div> 1276<ul> 1277<li><code>stream</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 1278<li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1279<li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1280<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked once the operation completes.</li> 1281<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for 1282the <code>'drain'</code> event to be emitted before continuing to write additional data; 1283otherwise <code>true</code>.</li> 1284</ul> 1285<p>The <code>readline.cursorTo()</code> method moves cursor to the specified position in a 1286given <a href="tty.html">TTY</a> <code>stream</code>.</p> 1287<h4><code>readline.moveCursor(stream, dx, dy[, callback])</code><span><a class="mark" href="#readlinemovecursorstream-dx-dy-callback" id="readlinemovecursorstream-dx-dy-callback">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline_movecursor_stream_dx_dy_callback"></a></h4> 1288<div class="api_metadata"> 1289<details class="changelog"><summary>History</summary> 1290<table> 1291<tbody><tr><th>Version</th><th>Changes</th></tr> 1292<tr><td>v18.0.0</td> 1293<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 1294<tr><td>v12.7.0</td> 1295<td><p>The stream's write() callback and return value are exposed.</p></td></tr> 1296<tr><td>v0.7.7</td> 1297<td><p><span>Added in: v0.7.7</span></p></td></tr> 1298</tbody></table> 1299</details> 1300</div> 1301<ul> 1302<li><code>stream</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 1303<li><code>dx</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1304<li><code>dy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1305<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked once the operation completes.</li> 1306<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for 1307the <code>'drain'</code> event to be emitted before continuing to write additional data; 1308otherwise <code>true</code>.</li> 1309</ul> 1310<p>The <code>readline.moveCursor()</code> method moves the cursor <em>relative</em> to its current 1311position in a given <a href="tty.html">TTY</a> <code>stream</code>.</p> 1312</section><section><h3><code>readline.emitKeypressEvents(stream[, interface])</code><span><a class="mark" href="#readlineemitkeypresseventsstream-interface" id="readlineemitkeypresseventsstream-interface">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline_emitkeypressevents_stream_interface"></a></h3> 1313<div class="api_metadata"> 1314<span>Added in: v0.7.7</span> 1315</div> 1316<ul> 1317<li><code>stream</code> <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a></li> 1318<li><code>interface</code> <a href="readline.html#class-interfaceconstructor" class="type"><readline.InterfaceConstructor></a></li> 1319</ul> 1320<p>The <code>readline.emitKeypressEvents()</code> method causes the given <a href="stream.html#readable-streams">Readable</a> 1321stream to begin emitting <code>'keypress'</code> events corresponding to received input.</p> 1322<p>Optionally, <code>interface</code> specifies a <code>readline.Interface</code> instance for which 1323autocompletion is disabled when copy-pasted input is detected.</p> 1324<p>If the <code>stream</code> is a <a href="tty.html">TTY</a>, then it must be in raw mode.</p> 1325<p>This is automatically called by any readline instance on its <code>input</code> if the 1326<code>input</code> is a terminal. Closing the <code>readline</code> instance does not stop 1327the <code>input</code> from emitting <code>'keypress'</code> events.</p> 1328<pre><code class="language-js">readline.<span class="hljs-title function_">emitKeypressEvents</span>(process.<span class="hljs-property">stdin</span>); 1329<span class="hljs-keyword">if</span> (process.<span class="hljs-property">stdin</span>.<span class="hljs-property">isTTY</span>) 1330 process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">setRawMode</span>(<span class="hljs-literal">true</span>);</code> <button class="copy-button">copy</button></pre> 1331</section><section><h3>Example: Tiny CLI<span><a class="mark" href="#example-tiny-cli" id="example-tiny-cli">#</a></span><a aria-hidden="true" class="legacy" id="readline_example_tiny_cli"></a></h3> 1332<p>The following example illustrates the use of <code>readline.Interface</code> class to 1333implement a small command-line interface:</p> 1334<pre><code class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>); 1335<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ 1336 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>, 1337 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>, 1338 <span class="hljs-attr">prompt</span>: <span class="hljs-string">'OHAI> '</span>, 1339}); 1340 1341rl.<span class="hljs-title function_">prompt</span>(); 1342 1343rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> { 1344 <span class="hljs-keyword">switch</span> (line.<span class="hljs-title function_">trim</span>()) { 1345 <span class="hljs-keyword">case</span> <span class="hljs-string">'hello'</span>: 1346 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'world!'</span>); 1347 <span class="hljs-keyword">break</span>; 1348 <span class="hljs-attr">default</span>: 1349 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Say what? I might have heard '<span class="hljs-subst">${line.trim()}</span>'`</span>); 1350 <span class="hljs-keyword">break</span>; 1351 } 1352 rl.<span class="hljs-title function_">prompt</span>(); 1353}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">() =></span> { 1354 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Have a great day!'</span>); 1355 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>); 1356});</code> <button class="copy-button">copy</button></pre> 1357</section><section><h3>Example: Read file stream line-by-Line<span><a class="mark" href="#example-read-file-stream-line-by-line" id="example-read-file-stream-line-by-line">#</a></span><a aria-hidden="true" class="legacy" id="readline_example_read_file_stream_line_by_line"></a></h3> 1358<p>A common use case for <code>readline</code> is to consume an input file one line at a 1359time. The easiest way to do so is leveraging the <a href="fs.html#class-fsreadstream"><code>fs.ReadStream</code></a> API as 1360well as a <code>for await...of</code> loop:</p> 1361<pre><code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 1362<span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>); 1363 1364<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">processLineByLine</span>(<span class="hljs-params"></span>) { 1365 <span class="hljs-keyword">const</span> fileStream = fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'input.txt'</span>); 1366 1367 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ 1368 <span class="hljs-attr">input</span>: fileStream, 1369 <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>, 1370 }); 1371 <span class="hljs-comment">// Note: we use the crlfDelay option to recognize all instances of CR LF</span> 1372 <span class="hljs-comment">// ('\r\n') in input.txt as a single line break.</span> 1373 1374 <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> line <span class="hljs-keyword">of</span> rl) { 1375 <span class="hljs-comment">// Each line in input.txt will be successively available here as `line`.</span> 1376 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Line from file: <span class="hljs-subst">${line}</span>`</span>); 1377 } 1378} 1379 1380<span class="hljs-title function_">processLineByLine</span>();</code> <button class="copy-button">copy</button></pre> 1381<p>Alternatively, one could use the <a href="#event-line"><code>'line'</code></a> event:</p> 1382<pre><code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 1383<span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>); 1384 1385<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ 1386 <span class="hljs-attr">input</span>: fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'sample.txt'</span>), 1387 <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>, 1388}); 1389 1390rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> { 1391 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Line from file: <span class="hljs-subst">${line}</span>`</span>); 1392});</code> <button class="copy-button">copy</button></pre> 1393<p>Currently, <code>for await...of</code> loop can be a bit slower. If <code>async</code> / <code>await</code> 1394flow and speed are both essential, a mixed approach can be applied:</p> 1395<pre><code class="language-js"><span class="hljs-keyword">const</span> { once } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 1396<span class="hljs-keyword">const</span> { createReadStream } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 1397<span class="hljs-keyword">const</span> { createInterface } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>); 1398 1399(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">processLineByLine</span>(<span class="hljs-params"></span>) { 1400 <span class="hljs-keyword">try</span> { 1401 <span class="hljs-keyword">const</span> rl = <span class="hljs-title function_">createInterface</span>({ 1402 <span class="hljs-attr">input</span>: <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'big-file.txt'</span>), 1403 <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>, 1404 }); 1405 1406 rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> { 1407 <span class="hljs-comment">// Process the line.</span> 1408 }); 1409 1410 <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(rl, <span class="hljs-string">'close'</span>); 1411 1412 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'File processed.'</span>); 1413 } <span class="hljs-keyword">catch</span> (err) { 1414 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 1415 } 1416})();</code> <button class="copy-button">copy</button></pre> 1417</section><section><h3>TTY keybindings<span><a class="mark" href="#tty-keybindings" id="tty-keybindings">#</a></span><a aria-hidden="true" class="legacy" id="readline_tty_keybindings"></a></h3> 1418<table> 1419 <tbody><tr> 1420 <th>Keybindings</th> 1421 <th>Description</th> 1422 <th>Notes</th> 1423 </tr> 1424 <tr> 1425 <td><kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Backspace</kbd></td> 1426 <td>Delete line left</td> 1427 <td>Doesn't work on Linux, Mac and Windows</td> 1428 </tr> 1429 <tr> 1430 <td><kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Delete</kbd></td> 1431 <td>Delete line right</td> 1432 <td>Doesn't work on Mac</td> 1433 </tr> 1434 <tr> 1435 <td><kbd>Ctrl</kbd>+<kbd>C</kbd></td> 1436 <td>Emit <code>SIGINT</code> or close the readline instance</td> 1437 <td></td> 1438 </tr> 1439 <tr> 1440 <td><kbd>Ctrl</kbd>+<kbd>H</kbd></td> 1441 <td>Delete left</td> 1442 <td></td> 1443 </tr> 1444 <tr> 1445 <td><kbd>Ctrl</kbd>+<kbd>D</kbd></td> 1446 <td>Delete right or close the readline instance in case the current line is empty / EOF</td> 1447 <td>Doesn't work on Windows</td> 1448 </tr> 1449 <tr> 1450 <td><kbd>Ctrl</kbd>+<kbd>U</kbd></td> 1451 <td>Delete from the current position to the line start</td> 1452 <td></td> 1453 </tr> 1454 <tr> 1455 <td><kbd>Ctrl</kbd>+<kbd>K</kbd></td> 1456 <td>Delete from the current position to the end of line</td> 1457 <td></td> 1458 </tr> 1459 <tr> 1460 <td><kbd>Ctrl</kbd>+<kbd>Y</kbd></td> 1461 <td>Yank (Recall) the previously deleted text</td> 1462 <td>Only works with text deleted by <kbd>Ctrl</kbd>+<kbd>U</kbd> or <kbd>Ctrl</kbd>+<kbd>K</kbd></td> 1463 </tr> 1464 <tr> 1465 <td><kbd>Meta</kbd>+<kbd>Y</kbd></td> 1466 <td>Cycle among previously deleted lines</td> 1467 <td>Only available when the last keystroke is <kbd>Ctrl</kbd>+<kbd>Y</kbd></td> 1468 </tr> 1469 <tr> 1470 <td><kbd>Ctrl</kbd>+<kbd>A</kbd></td> 1471 <td>Go to start of line</td> 1472 <td></td> 1473 </tr> 1474 <tr> 1475 <td><kbd>Ctrl</kbd>+<kbd>E</kbd></td> 1476 <td>Go to end of line</td> 1477 <td></td> 1478 </tr> 1479 <tr> 1480 <td><kbd>Ctrl</kbd>+<kbd>B</kbd></td> 1481 <td>Back one character</td> 1482 <td></td> 1483 </tr> 1484 <tr> 1485 <td><kbd>Ctrl</kbd>+<kbd>F</kbd></td> 1486 <td>Forward one character</td> 1487 <td></td> 1488 </tr> 1489 <tr> 1490 <td><kbd>Ctrl</kbd>+<kbd>L</kbd></td> 1491 <td>Clear screen</td> 1492 <td></td> 1493 </tr> 1494 <tr> 1495 <td><kbd>Ctrl</kbd>+<kbd>N</kbd></td> 1496 <td>Next history item</td> 1497 <td></td> 1498 </tr> 1499 <tr> 1500 <td><kbd>Ctrl</kbd>+<kbd>P</kbd></td> 1501 <td>Previous history item</td> 1502 <td></td> 1503 </tr> 1504 <tr> 1505 <td><kbd>Ctrl</kbd>+<kbd>-</kbd></td> 1506 <td>Undo previous change</td> 1507 <td>Any keystroke that emits key code <code>0x1F</code> will do this action. 1508 In many terminals, for example <code>xterm</code>, 1509 this is bound to <kbd>Ctrl</kbd>+<kbd>-</kbd>.</td> 1510 </tr> 1511 <tr> 1512 <td><kbd>Ctrl</kbd>+<kbd>6</kbd></td> 1513 <td>Redo previous change</td> 1514 <td>Many terminals don't have a default redo keystroke. 1515 We choose key code <code>0x1E</code> to perform redo. 1516 In <code>xterm</code>, it is bound to <kbd>Ctrl</kbd>+<kbd>6</kbd> 1517 by default.</td> 1518 </tr> 1519 <tr> 1520 <td><kbd>Ctrl</kbd>+<kbd>Z</kbd></td> 1521 <td>Moves running process into background. Type 1522 <code>fg</code> and press <kbd>Enter</kbd> 1523 to return.</td> 1524 <td>Doesn't work on Windows</td> 1525 </tr> 1526 <tr> 1527 <td><kbd>Ctrl</kbd>+<kbd>W</kbd> or <kbd>Ctrl</kbd> 1528 +<kbd>Backspace</kbd></td> 1529 <td>Delete backward to a word boundary</td> 1530 <td><kbd>Ctrl</kbd>+<kbd>Backspace</kbd> Doesn't 1531 work on Linux, Mac and Windows</td> 1532 </tr> 1533 <tr> 1534 <td><kbd>Ctrl</kbd>+<kbd>Delete</kbd></td> 1535 <td>Delete forward to a word boundary</td> 1536 <td>Doesn't work on Mac</td> 1537 </tr> 1538 <tr> 1539 <td><kbd>Ctrl</kbd>+<kbd>Left arrow</kbd> or 1540 <kbd>Meta</kbd>+<kbd>B</kbd></td> 1541 <td>Word left</td> 1542 <td><kbd>Ctrl</kbd>+<kbd>Left arrow</kbd> Doesn't work 1543 on Mac</td> 1544 </tr> 1545 <tr> 1546 <td><kbd>Ctrl</kbd>+<kbd>Right arrow</kbd> or 1547 <kbd>Meta</kbd>+<kbd>F</kbd></td> 1548 <td>Word right</td> 1549 <td><kbd>Ctrl</kbd>+<kbd>Right arrow</kbd> Doesn't work 1550 on Mac</td> 1551 </tr> 1552 <tr> 1553 <td><kbd>Meta</kbd>+<kbd>D</kbd> or <kbd>Meta</kbd> 1554 +<kbd>Delete</kbd></td> 1555 <td>Delete word right</td> 1556 <td><kbd>Meta</kbd>+<kbd>Delete</kbd> Doesn't work 1557 on windows</td> 1558 </tr> 1559 <tr> 1560 <td><kbd>Meta</kbd>+<kbd>Backspace</kbd></td> 1561 <td>Delete word left</td> 1562 <td>Doesn't work on Mac</td> 1563 </tr> 1564</tbody></table></section> 1565 <!-- API END --> 1566 </div> 1567 </div> 1568 </div> 1569</body> 1570</html> 1571