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