Node.js readline Module
Node.js's readline The module is used to read data from a readable stream (such as process.stdin) an interface for reading data line by line. It provides a simple way to handle command-line input, making it very suitable for creating interactive command-line applications.
Basic Usage
Import the readline module
To use readline module, you first need to import it:
const
readline
=
require
(
'readline'
)
;
Create a readline interface
creation readline.Interface The instance is the core of using this module:
Example
const
rl
=
readline.
createInterface
(
{
input
:
process.
stdin
,
output
:
process.
stdout
}
)
;
Here we create an interface that reads data from standard input (the keyboard) and writes output to standard output (the console).
Main methods and events
Read input line by line
Use question() The method can ask the user a question and wait for an answer:
Example
rl.
question
(
'What is your name?'
,
(
answer
)
=>
{
console.
log
(
`Hello, $
{
answer
}
!`
)
;
rl.
close
(
)
;
}
)
;
Listen for the line event
Triggered when the user types a line and presses Enter line event:
Example
rl.
on
(
'line'
,
(
input
)
=>
{
console.
log
(
`Received: $
{
input
}
`
)
;
if
(
input
===
'exit'
)
{
rl.
close
(
)
;
}
}
)
;
Deactivate the interface
Use close() The method closes the interface and releases resources:
Example
rl.
close
(
)
;
Triggered when the interface is closed close event:
Example
rl.
on
(
'close'
,
(
)
=>
{
console.
log
(
'Goodbye!'
)
;
process.
exit
(
0
)
;
}
)
;
Advanced features
History records
readline The module supports command history:
Example
rl.
history
=
[
'command1'
,
'command2'
]
;
// Set up command history
Users can browse command history with the up and down arrow keys.
Custom completion
You can implement custom Tab completion:
Example
rl.
on
(
'completer'
,
(
line
)
=>
{
const
completions
=
[
'help'
,
'exit'
,
'show'
]
;
const
hits
=
completions.
filter
(
(
c
)
=>
c.
startsWith
(
line
)
)
;
return
[
hits.
length
?
hits
:
completions
,
line
]
;
}
)
;
Cursor control
readline Several methods are provided to control the cursor:
Example
readline.
cursorTo
(
process.
stdout
,
10
,
5
)
;
// Move the cursor to column 10, row 5
readline.
clearLine
(
process.
stdout
,
0
)
;
// Clear the current line
readline.
clearScreenDown
(
process.
stdout
)
;
// Clear the content below the cursor
Practical Application Examples
Create a simple command-line Q&A program
Example
const
readline
=
require
(
'readline'
)
;
const
rl
=
readline.
createInterface
(
{
input
:
process.
stdin
,
output
:
process.
stdout
}
)
;
const
questions
=
[
'What is your name?'
,
'How old are you?'
,
'What is your favorite programming language?'
]
;
const
answers
=
[
]
;
function
askQuestion
(
i
=
0
)
{
if
(
i
=
questions.
length
)
{
console.
log
(
'Thank you for your answer:'
,
answers
)
;
return
rl.
close
(
)
;
}
rl.
question
(
questions
[
i
]
,
(
answer
)
=>
{
answers.
push
(
answer
)
;
askQuestion
(
i
1
)
;
}
)
;
}
askQuestion
(
)
;
Create a simple command-line calculator
Example
const
readline
=
require
(
'readline'
)
;
const
rl
=
readline.
createInterface
(
{
input
:
process.
stdin
,
output
:
process.
stdout
}
)
;
function
calculator
(
)
{
rl.
question
(
'Enter an expression (e.g. 2 + 3, or type exit to quit): '
,
(
input
)
=>
{
if
(
input
===
'exit'
)
{
return
rl.
close
(
)
;
}
try
{
const
result
=
eval
(
input
)
;
// Note: avoid using eval directly in real applications
console.
log
(
`Result
:
$
{
result
}
`
)
;
}
catch
(
e
)
{
console.
log
(
'Invalid expression'
)
;
}
calculator
(
)
;
// Call recursively to continue the next calculation
}
)
;
}
calculator
(
)
;
Security considerations
-
Avoid using eval(): the calculator example above uses
eval(), which is unsafe in production because it executes any JavaScript code that is entered. In real applications, you should use a dedicated expression parser. -
Input validation: always validate user input to prevent injection attacks and other security issues.
-
Error handling: add proper error handling to all asynchronous operations.
Summary
Node.js's readline The module is a powerful tool for building interactive command-line applications. With it, we can:
- Easily get user input
- Create question-and-answer interfaces
- Implement command history
- Add Tab completion
- Control terminal cursor and output
Mastering the readline The module is an important step in developing Node.js command-line tools; we hope this article helps you understand and use it.
Node.js Built-in Modules