{ "type": "module", "source": "doc/api/readline.md", "modules": [ { "textRaw": "Readline", "name": "readline", "introduced_in": "v0.10.0", "stability": 2, "stabilityText": "Stable", "desc": "
Source Code: lib/readline.js
\nThe readline
module provides an interface for reading data from a Readable\nstream (such as process.stdin
) one line at a time. It can be accessed\nusing:
const readline = require('readline');\n
\nThe following simple example illustrates the basic use of the readline
module.
const readline = require('readline');\n\nconst rl = readline.createInterface({\n input: process.stdin,\n output: process.stdout\n});\n\nrl.question('What do you think of Node.js? ', (answer) => {\n // TODO: Log the answer in a database\n console.log(`Thank you for your valuable feedback: ${answer}`);\n\n rl.close();\n});\n
\nOnce this code is invoked, the Node.js application will not terminate until the\nreadline.Interface
is closed because the interface waits for data to be\nreceived on the input
stream.
Instances of the readline.Interface
class are constructed using the\nreadline.createInterface()
method. Every instance is associated with a\nsingle input
Readable stream and a single output
Writable stream.\nThe output
stream is used to print prompts for user input that arrives on,\nand is read from, the input
stream.
The 'close'
event is emitted when one of the following occur:
rl.close()
method is called and the readline.Interface
instance has\nrelinquished control over the input
and output
streams;input
stream receives its 'end'
event;input
stream receives Ctrl+D to signal\nend-of-transmission (EOT);input
stream receives Ctrl+C to signal SIGINT
\nand there is no 'SIGINT'
event listener registered on the\nreadline.Interface
instance.The listener function is called without passing any arguments.
\nThe readline.Interface
instance is finished once the 'close'
event is\nemitted.
The 'line'
event is emitted whenever the input
stream receives an\nend-of-line input (\\n
, \\r
, or \\r\\n
). This usually occurs when the user\npresses Enter or Return.
The listener function is called with a string containing the single line of\nreceived input.
\nrl.on('line', (input) => {\n console.log(`Received: ${input}`);\n});\n
"
},
{
"textRaw": "Event: `'history'`",
"type": "event",
"name": "history",
"meta": {
"added": [
"v15.8.0"
],
"changes": []
},
"params": [],
"desc": "The 'history'
event is emitted whenever the history array has changed.
The listener function is called with an array containing the history array.\nIt will reflect all changes, added lines and removed lines due to\nhistorySize
and removeHistoryDuplicates
.
The primary purpose is to allow a listener to persist the history.\nIt is also possible for the listener to change the history object. This\ncould be useful to prevent certain lines to be added to the history, like\na password.
\nrl.on('history', (history) => {\n console.log(`Received: ${history}`);\n});\n
"
},
{
"textRaw": "Event: `'pause'`",
"type": "event",
"name": "pause",
"meta": {
"added": [
"v0.7.5"
],
"changes": []
},
"params": [],
"desc": "The 'pause'
event is emitted when one of the following occur:
input
stream is paused.input
stream is not paused and receives the 'SIGCONT'
event. (See\nevents 'SIGTSTP'
and 'SIGCONT'
.)The listener function is called without passing any arguments.
\nrl.on('pause', () => {\n console.log('Readline paused.');\n});\n
"
},
{
"textRaw": "Event: `'resume'`",
"type": "event",
"name": "resume",
"meta": {
"added": [
"v0.7.5"
],
"changes": []
},
"params": [],
"desc": "The 'resume'
event is emitted whenever the input
stream is resumed.
The listener function is called without passing any arguments.
\nrl.on('resume', () => {\n console.log('Readline resumed.');\n});\n
"
},
{
"textRaw": "Event: `'SIGCONT'`",
"type": "event",
"name": "SIGCONT",
"meta": {
"added": [
"v0.7.5"
],
"changes": []
},
"params": [],
"desc": "The 'SIGCONT'
event is emitted when a Node.js process previously moved into\nthe background using Ctrl+Z (i.e. SIGTSTP
) is then\nbrought back to the foreground using fg(1p)
.
If the input
stream was paused before the SIGTSTP
request, this event will\nnot be emitted.
The listener function is invoked without passing any arguments.
\nrl.on('SIGCONT', () => {\n // `prompt` will automatically resume the stream\n rl.prompt();\n});\n
\nThe 'SIGCONT'
event is not supported on Windows.
The 'SIGINT'
event is emitted whenever the input
stream receives a\nCtrl+C input, known typically as SIGINT
. If there are no 'SIGINT'
\nevent listeners registered when the input
stream receives a SIGINT
, the\n'pause'
event will be emitted.
The listener function is invoked without passing any arguments.
\nrl.on('SIGINT', () => {\n rl.question('Are you sure you want to exit? ', (answer) => {\n if (answer.match(/^y(es)?$/i)) rl.pause();\n });\n});\n
"
},
{
"textRaw": "Event: `'SIGTSTP'`",
"type": "event",
"name": "SIGTSTP",
"meta": {
"added": [
"v0.7.5"
],
"changes": []
},
"params": [],
"desc": "The 'SIGTSTP'
event is emitted when the input
stream receives a\nCtrl+Z input, typically known as SIGTSTP
. If there are\nno 'SIGTSTP'
event listeners registered when the input
stream receives a\nSIGTSTP
, the Node.js process will be sent to the background.
When the program is resumed using fg(1p)
, the 'pause'
and 'SIGCONT'
events\nwill be emitted. These can be used to resume the input
stream.
The 'pause'
and 'SIGCONT'
events will not be emitted if the input
was\npaused before the process was sent to the background.
The listener function is invoked without passing any arguments.
\nrl.on('SIGTSTP', () => {\n // This will override SIGTSTP and prevent the program from going to the\n // background.\n console.log('Caught SIGTSTP.');\n});\n
\nThe 'SIGTSTP'
event is not supported on Windows.
The rl.close()
method closes the readline.Interface
instance and\nrelinquishes control over the input
and output
streams. When called,\nthe 'close'
event will be emitted.
Calling rl.close()
does not immediately stop other events (including 'line'
)\nfrom being emitted by the readline.Interface
instance.
The rl.pause()
method pauses the input
stream, allowing it to be resumed\nlater if necessary.
Calling rl.pause()
does not immediately pause other events (including\n'line'
) from being emitted by the readline.Interface
instance.
The rl.prompt()
method writes the readline.Interface
instances configured\nprompt
to a new line in output
in order to provide a user with a new\nlocation at which to provide input.
When called, rl.prompt()
will resume the input
stream if it has been\npaused.
If the readline.Interface
was created with output
set to null
or\nundefined
the prompt is not written.
The rl.question()
method displays the query
by writing it to the output
,\nwaits for user input to be provided on input
, then invokes the callback
\nfunction passing the provided input as the first argument.
When called, rl.question()
will resume the input
stream if it has been\npaused.
If the readline.Interface
was created with output
set to null
or\nundefined
the query
is not written.
The callback
function passed to rl.question()
does not follow the typical\npattern of accepting an Error
object or null
as the first argument.\nThe callback
is called with the provided answer as the only argument.
Example usage:
\nrl.question('What is your favorite food? ', (answer) => {\n console.log(`Oh, so your favorite food is ${answer}`);\n});\n
\nUsing an AbortController
to cancel a question.
const ac = new AbortController();\nconst signal = ac.signal;\n\nrl.question('What is your favorite food? ', { signal }, (answer) => {\n console.log(`Oh, so your favorite food is ${answer}`);\n});\n\nsignal.addEventListener('abort', () => {\n console.log('The food question timed out');\n}, { once: true });\n\nsetTimeout(() => ac.abort(), 10000);\n
\nIf this method is invoked as it's util.promisify()ed version, it returns a\nPromise that fulfills with the answer. If the question is canceled using\nan AbortController
it will reject with an AbortError
.
const util = require('util');\nconst question = util.promisify(rl.question).bind(rl);\n\nasync function questionExample() {\n try {\n const answer = await question('What is you favorite food? ');\n console.log(`Oh, so your favorite food is ${answer}`);\n } catch (err) {\n console.error('Question rejected', err);\n }\n}\nquestionExample();\n
"
},
{
"textRaw": "`rl.resume()`",
"type": "method",
"name": "resume",
"meta": {
"added": [
"v0.3.4"
],
"changes": []
},
"signatures": [
{
"params": []
}
],
"desc": "The rl.resume()
method resumes the input
stream if it has been paused.
The rl.setPrompt()
method sets the prompt that will be written to output
\nwhenever rl.prompt()
is called.
The rl.getPrompt()
method returns the current prompt used by rl.prompt()
.
The rl.write()
method will write either data
or a key sequence identified\nby key
to the output
. The key
argument is supported only if output
is\na TTY text terminal. See TTY keybindings for a list of key\ncombinations.
If key
is specified, data
is ignored.
When called, rl.write()
will resume the input
stream if it has been\npaused.
If the readline.Interface
was created with output
set to null
or\nundefined
the data
and key
are not written.
rl.write('Delete this!');\n// Simulate Ctrl+U to delete the line written previously\nrl.write(null, { ctrl: true, name: 'u' });\n
\nThe rl.write()
method will write the data to the readline
Interface
's\ninput
as if it were provided by the user.
Create an AsyncIterator
object that iterates through each line in the input\nstream as a string. This method allows asynchronous iteration of\nreadline.Interface
objects through for await...of
loops.
Errors in the input stream are not forwarded.
\nIf the loop is terminated with break
, throw
, or return
,\nrl.close()
will be called. In other words, iterating over a\nreadline.Interface
will always consume the input stream fully.
Performance is not on par with the traditional 'line'
event API. Use 'line'
\ninstead for performance-sensitive applications.
async function processLineByLine() {\n const rl = readline.createInterface({\n // ...\n });\n\n for await (const line of rl) {\n // Each line in the readline input will be successively available here as\n // `line`.\n }\n}\n
\nreadline.createInterface()
will start to consume the input stream once\ninvoked. Having asynchronous operations between interface creation and\nasynchronous iteration may result in missed lines.
Returns the real position of the cursor in relation to the input\nprompt + string. Long input (wrapping) strings, as well as multiple\nline prompts are included in the calculations.
" } ], "properties": [ { "textRaw": "`line` {string}", "type": "string", "name": "line", "meta": { "added": [ "v0.1.98" ], "changes": [ { "version": "v15.8.0", "pr-url": "https://github.com/nodejs/node/pull/33676", "description": "Value will always be a string, never undefined." } ] }, "desc": "The current input data being processed by node.
\nThis can be used when collecting input from a TTY stream to retrieve the\ncurrent value that has been processed thus far, prior to the line
event\nbeing emitted. Once the line
event has been emitted, this property will\nbe an empty string.
Be aware that modifying the value during the instance runtime may have\nunintended consequences if rl.cursor
is not also controlled.
If not using a TTY stream for input, use the 'line'
event.
One possible use case would be as follows:
\nconst values = ['lorem ipsum', 'dolor sit amet'];\nconst rl = readline.createInterface(process.stdin);\nconst showResults = debounce(() => {\n console.log(\n '\\n',\n values.filter((val) => val.startsWith(rl.line)).join(' ')\n );\n}, 300);\nprocess.stdin.on('keypress', (c, k) => {\n showResults();\n});\n
"
},
{
"textRaw": "`cursor` {number|undefined}",
"type": "number|undefined",
"name": "cursor",
"meta": {
"added": [
"v0.1.98"
],
"changes": []
},
"desc": "The cursor position relative to rl.line
.
This will track where the current cursor lands in the input string, when\nreading input from a TTY stream. The position of cursor determines the\nportion of the input string that will be modified as input is processed,\nas well as the column where the terminal caret will be rendered.
" } ] } ], "methods": [ { "textRaw": "`readline.clearLine(stream, dir[, callback])`", "type": "method", "name": "clearLine", "meta": { "added": [ "v0.7.7" ], "changes": [ { "version": "v12.7.0", "pr-url": "https://github.com/nodejs/node/pull/28674", "description": "The stream's write() callback and return value are exposed." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean} `false` if `stream` wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`.", "name": "return", "type": "boolean", "desc": "`false` if `stream` wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`." }, "params": [ { "textRaw": "`stream` {stream.Writable}", "name": "stream", "type": "stream.Writable" }, { "textRaw": "`dir` {number}", "name": "dir", "type": "number", "options": [ { "textRaw": "`-1`: to the left from cursor", "name": "-1", "desc": "to the left from cursor" }, { "textRaw": "`1`: to the right from cursor", "name": "1", "desc": "to the right from cursor" }, { "textRaw": "`0`: the entire line", "name": "0", "desc": "the entire line" } ] }, { "textRaw": "`callback` {Function} Invoked once the operation completes.", "name": "callback", "type": "Function", "desc": "Invoked once the operation completes." } ] } ], "desc": "The readline.clearLine()
method clears current line of given TTY stream\nin a specified direction identified by dir
.
The readline.clearScreenDown()
method clears the given TTY stream from\nthe current position of the cursor down.
The readline.createInterface()
method creates a new readline.Interface
\ninstance.
const readline = require('readline');\nconst rl = readline.createInterface({\n input: process.stdin,\n output: process.stdout\n});\n
\nOnce the readline.Interface
instance is created, the most common case is to\nlisten for the 'line'
event:
rl.on('line', (line) => {\n console.log(`Received: ${line}`);\n});\n
\nIf terminal
is true
for this instance then the output
stream will get\nthe best compatibility if it defines an output.columns
property and emits\na 'resize'
event on the output
if or when the columns ever change\n(process.stdout
does this automatically when it is a TTY).
When creating a readline.Interface
using stdin
as input, the program\nwill not terminate until it receives EOF
(Ctrl+D on\nLinux/macOS, Ctrl+Z followed by Return on\nWindows).\nIf you want your application to exit without waiting for user input, you can\nunref()
the standard input stream:
process.stdin.unref();\n
",
"modules": [
{
"textRaw": "Use of the `completer` function",
"name": "use_of_the_`completer`_function",
"desc": "The completer
function takes the current line entered by the user\nas an argument, and returns an Array
with 2 entries:
Array
with matching entries for the completion.For instance: [[substr1, substr2, ...], originalsubstring]
.
function completer(line) {\n const completions = '.help .error .exit .quit .q'.split(' ');\n const hits = completions.filter((c) => c.startsWith(line));\n // Show all completions if none found\n return [hits.length ? hits : completions, line];\n}\n
\nThe completer
function can be called asynchronously if it accepts two\narguments:
function completer(linePartial, callback) {\n callback(null, [['123'], linePartial]);\n}\n
",
"type": "module",
"displayName": "Use of the `completer` function"
}
]
},
{
"textRaw": "`readline.cursorTo(stream, x[, y][, callback])`",
"type": "method",
"name": "cursorTo",
"meta": {
"added": [
"v0.7.7"
],
"changes": [
{
"version": "v12.7.0",
"pr-url": "https://github.com/nodejs/node/pull/28674",
"description": "The stream's write() callback and return value are exposed."
}
]
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean} `false` if `stream` wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`.",
"name": "return",
"type": "boolean",
"desc": "`false` if `stream` wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`."
},
"params": [
{
"textRaw": "`stream` {stream.Writable}",
"name": "stream",
"type": "stream.Writable"
},
{
"textRaw": "`x` {number}",
"name": "x",
"type": "number"
},
{
"textRaw": "`y` {number}",
"name": "y",
"type": "number"
},
{
"textRaw": "`callback` {Function} Invoked once the operation completes.",
"name": "callback",
"type": "Function",
"desc": "Invoked once the operation completes."
}
]
}
],
"desc": "The readline.cursorTo()
method moves cursor to the specified position in a\ngiven TTY stream
.
The readline.emitKeypressEvents()
method causes the given Readable\nstream to begin emitting 'keypress'
events corresponding to received input.
Optionally, interface
specifies a readline.Interface
instance for which\nautocompletion is disabled when copy-pasted input is detected.
If the stream
is a TTY, then it must be in raw mode.
This is automatically called by any readline instance on its input
if the\ninput
is a terminal. Closing the readline
instance does not stop\nthe input
from emitting 'keypress'
events.
readline.emitKeypressEvents(process.stdin);\nif (process.stdin.isTTY)\n process.stdin.setRawMode(true);\n
"
},
{
"textRaw": "`readline.moveCursor(stream, dx, dy[, callback])`",
"type": "method",
"name": "moveCursor",
"meta": {
"added": [
"v0.7.7"
],
"changes": [
{
"version": "v12.7.0",
"pr-url": "https://github.com/nodejs/node/pull/28674",
"description": "The stream's write() callback and return value are exposed."
}
]
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean} `false` if `stream` wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`.",
"name": "return",
"type": "boolean",
"desc": "`false` if `stream` wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`."
},
"params": [
{
"textRaw": "`stream` {stream.Writable}",
"name": "stream",
"type": "stream.Writable"
},
{
"textRaw": "`dx` {number}",
"name": "dx",
"type": "number"
},
{
"textRaw": "`dy` {number}",
"name": "dy",
"type": "number"
},
{
"textRaw": "`callback` {Function} Invoked once the operation completes.",
"name": "callback",
"type": "Function",
"desc": "Invoked once the operation completes."
}
]
}
],
"desc": "The readline.moveCursor()
method moves the cursor relative to its current\nposition in a given TTY stream
.
The following example illustrates the use of readline.Interface
class to\nimplement a small command-line interface:
const readline = require('readline');\nconst rl = readline.createInterface({\n input: process.stdin,\n output: process.stdout,\n prompt: 'OHAI> '\n});\n\nrl.prompt();\n\nrl.on('line', (line) => {\n switch (line.trim()) {\n case 'hello':\n console.log('world!');\n break;\n default:\n console.log(`Say what? I might have heard '${line.trim()}'`);\n break;\n }\n rl.prompt();\n}).on('close', () => {\n console.log('Have a great day!');\n process.exit(0);\n});\n
\nA common use case for readline
is to consume an input file one line at a\ntime. The easiest way to do so is leveraging the fs.ReadStream
API as\nwell as a for await...of
loop:
const fs = require('fs');\nconst readline = require('readline');\n\nasync function processLineByLine() {\n const fileStream = fs.createReadStream('input.txt');\n\n const rl = readline.createInterface({\n input: fileStream,\n crlfDelay: Infinity\n });\n // Note: we use the crlfDelay option to recognize all instances of CR LF\n // ('\\r\\n') in input.txt as a single line break.\n\n for await (const line of rl) {\n // Each line in input.txt will be successively available here as `line`.\n console.log(`Line from file: ${line}`);\n }\n}\n\nprocessLineByLine();\n
\nAlternatively, one could use the 'line'
event:
const fs = require('fs');\nconst readline = require('readline');\n\nconst rl = readline.createInterface({\n input: fs.createReadStream('sample.txt'),\n crlfDelay: Infinity\n});\n\nrl.on('line', (line) => {\n console.log(`Line from file: ${line}`);\n});\n
\nCurrently, for await...of
loop can be a bit slower. If async
/ await
\nflow and speed are both essential, a mixed approach can be applied:
const { once } = require('events');\nconst { createReadStream } = require('fs');\nconst { createInterface } = require('readline');\n\n(async function processLineByLine() {\n try {\n const rl = createInterface({\n input: createReadStream('big-file.txt'),\n crlfDelay: Infinity\n });\n\n rl.on('line', (line) => {\n // Process the line.\n });\n\n await once(rl, 'close');\n\n console.log('File processed.');\n } catch (err) {\n console.error(err);\n }\n})();\n
"
}
],
"modules": [
{
"textRaw": "TTY keybindings",
"name": "tty_keybindings",
"desc": "Keybindings | \nDescription | \nNotes | \n
---|---|---|
Ctrl+Shift+Backspace | \nDelete line left | \nDoesn't work on Linux, Mac and Windows | \n
Ctrl+Shift+Delete | \nDelete line right | \nDoesn't work on Mac | \n
Ctrl+C | \nEmit SIGINT or close the readline instance | \n \n |
Ctrl+H | \nDelete left | \n\n |
Ctrl+D | \nDelete right or close the readline instance in case the current line is empty / EOF | \nDoesn't work on Windows | \n
Ctrl+U | \nDelete from the current position to the line start | \n\n |
Ctrl+K | \nDelete from the current position to the end of line | \n\n |
Ctrl+A | \nGo to start of line | \n\n |
Ctrl+E | \nGo to to end of line | \n\n |
Ctrl+B | \nBack one character | \n\n |
Ctrl+F | \nForward one character | \n\n |
Ctrl+L | \nClear screen | \n\n |
Ctrl+N | \nNext history item | \n\n |
Ctrl+P | \nPrevious history item | \n\n |
Ctrl+Z | \nMoves running process into background. Type\n fg and press Enter\n to return. | \n Doesn't work on Windows | \n
Ctrl+W or Ctrl\n +Backspace | \nDelete backward to a word boundary | \nCtrl+Backspace Doesn't\n work on Linux, Mac and Windows | \n
Ctrl+Delete | \nDelete forward to a word boundary | \nDoesn't work on Mac | \n
Ctrl+Left arrow or\n Meta+B | \nWord left | \nCtrl+Left arrow Doesn't work\n on Mac | \n
Ctrl+Right arrow or\n Meta+F | \nWord right | \nCtrl+Right arrow Doesn't work\n on Mac | \n
Meta+D or Meta\n +Delete | \nDelete word right | \nMeta+Delete Doesn't work\n on windows | \n
Meta+Backspace | \nDelete word left | \nDoesn't work on Mac | \n