Command-Line Input in Node.js
Learn how to accept input from users and process it in Node.js CLI applications.
1. What Is Command-Line Input?
Command-line input means accepting information from a user through a terminal. For example, a program may ask for a student's name, age, course, or a task description.
In Node.js, we can use the built-in
readline module to read input from the terminal.
This module provides methods to ask questions and receive
the user's responses.
2. How Command-Line Input Works
The following diagram shows how a CLI application receives and processes information from a user.
Node.js Program
Asks a question
Terminal
Displays the question
User Input
User types an answer
Processing and Output
Program uses the answer and displays a result
3. What Is the Readline Module?
The readline module is a built-in Node.js module
used to read data from a readable stream, such as terminal input.
It can display questions and receive responses from users.
Because it is built into Node.js, you do not need to install an additional npm package to use it.
Basic Syntax
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
Understanding the Code
| Code | Meaning |
|---|---|
require("readline") |
Loads the built-in readline module. |
createInterface() |
Creates an interface for reading and writing. |
process.stdin |
Receives input from the terminal. |
process.stdout |
Displays output in the terminal. |
4. Example: Accept the User's Name
Let's create a program that asks the user for their name and displays a welcome message.
Step 1: Create a File
Create a JavaScript file named name.js.
Step 2: Write the Code
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter your name: ", function(name) {
console.log("Welcome, " + name + "!");
rl.close();
});
Step 3: Run the Program
node name.js
Expected Output
Enter your name: Rahul
Welcome, Rahul!
How Does It Work?
First, rl.question() displays a question in the
terminal. The user enters Rahul. The callback
function receives the entered name in the
name parameter and displays the welcome message.
Finally, rl.close() closes the readline interface.
5. Example: Accept Multiple Inputs
We can ask more than one question by placing another
rl.question() inside the first question's callback.
Program: Student Details
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter student name: ", function(name) {
rl.question("Enter course name: ", function(course) {
console.log("\n--- Student Details ---");
console.log("Name: " + name);
console.log("Course: " + course);
rl.close();
});
});
Expected Output
Enter student name: Rahul
Enter course name: Node.js
--- Student Details ---
Name: Rahul
Course: Node.js
Here, the application first receives the student's name. Then it asks for the course name. After receiving both inputs, it displays the student details.
6. Multiple Input Flow
This diagram shows the order in which the student details program executes.
Ask Student Name
rl.question()
Store Name in a Parameter
Example: Rahul
Ask Course Name
rl.question()
Display Student Details
Print the name and course
Close Interface
rl.close()
7. Important: User Input Is a String
Values received through rl.question() are strings.
If you want to perform mathematical calculations, convert the
input to a number first.
Example: Add Two Numbers
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter first number: ", function(first) {
rl.question("Enter second number: ", function(second) {
const num1 = Number(first);
const num2 = Number(second);
if (
first.trim() === "" ||
second.trim() === "" ||
!Number.isFinite(num1) ||
!Number.isFinite(num2)
) {
console.log("Please enter valid numbers.");
} else {
console.log("Sum: " + (num1 + num2));
}
rl.close();
});
});
Expected Output
Enter first number: 10
Enter second number: 20
Sum: 30
The Number() function converts the entered strings
into numbers. The program then adds the numbers and displays
the result. The validation prevents empty or non-numeric
entries from being treated as valid numbers.
"10" + "20" produces "1020"
because both values are strings. Using
Number("10") + Number("20") produces
30.
8. Important Readline Methods
| Method or Property | Purpose |
|---|---|
readline.createInterface() |
Creates the readline interface. |
rl.question() |
Displays a question and receives a response. |
process.stdin |
Represents standard input. |
process.stdout |
Represents standard output. |
rl.close() |
Closes the readline interface. |
9. Practice Tasks
Task 1: Personal Information
Ask the user for their name, age, and city. Display all three values in the terminal.
Hint: Use three
rl.question() calls and close the interface
after collecting the final value.
Task 2: Simple Calculator
Ask the user for two numbers and display their sum, difference, and product.
Hint: Convert the input to numbers using
Number() before performing calculations.
Summary
Command-line input allows a Node.js application to receive
information directly from the user through a terminal. The
built-in readline module provides
createInterface() to configure terminal input
and output, while rl.question() asks questions
and receives responses. The rl.close() method
closes the interface when input collection is complete.
We learned how to accept a user's name, collect multiple values, and add two numbers after converting string input into numeric values. These skills are useful when building practical CLI projects such as a student record system or a task manager at CIIT Training Institute.