Node.js includes built-in core modules that enable application development without custom implementations, especially for handling user input in command-line applications.
- Node.js provides core modules alongside CommonJS and ES modules.
- Input modules are essential for building CLI-based applications.
- Used to capture user input such as package name, author, and license.
- Commonly handled using built-in modules like process and readline.
Input Module
The input module, using the readline and process modules, allows developers to gather input from the user directly through the command line. This is particularly useful for interactive CLI programs.
Components
The input module mainly consists of the following core Node.js components:
1. Process Module
A global module in Node.js that provides information about the currently running process.
- Accesses command-line arguments and environment variables.
- Handles standard input and output streams.
Note: The process module is a global object in Node.js and does not require an explicit import.
Implementing the process Module
process.argv returns an array containing command-line arguments passed when the Node.js process starts.
// index.js
const args = process.argv.slice(2);
const name = args[0];
const email = args[1];
const age = args[2];
console.log("Your entered details:", {
name,
email,
age
});
Running the program:
node index.js Gfg gfg@example.com 22Output:
Your entered details: { name: 'Gfg', email: 'gfg@example.com', age: '22' }- process.argv captures command-line arguments passed during script execution.
- Inputs are read instantly without prompting the user, making it ideal for automation scripts and CI/CD tasks.
2. Readline Module
Available in Node.js core, the readline module enables interactive, line-by-line input from the command line.
- It reads user input from process.stdin and writes prompts to process.stdout.
- It is commonly used to build interactive CLI programs that require user responses.
Implementing the readline Module
To use the readline module in Node.js, follow these steps:
//index.js
//Readline - Grab input from user
import { createInterface } from "readline" ;
const readline = createInterface({
input: process.stdin,
output: process.stdout
});
readline.question("What is your name? ", (ans) => {
console.log("Answer :", ans );
readline.close();
});
Output:
What is your name ?
GFG
Answer : GFG
- createInterface connects process.stdin and process.stdout to read user input from the command line and display prompts.
- readline.question() prompts the user, captures the input, logs the answer, and then closes the interface.
Handling Multiple Questions
A simple CLI program that collects name, email, and age of the user step by step.
//index.js
//Readline - Grab input from user
import { createInterface } from "readline" ;
const readline = createInterface({
input: process.stdin,
output: process.stdout
});
const details = {name : "", email : "", age : ""}
readline.question("What is your name? ", (nameAns) => {
details.name = nameAns;
readline.question("What is your email? ", (emailAns) => {
details.email = emailAns;
readline.question("What is your age? ", (ageAns) => {
details.age = ageAns;
console.log("Your entered details:", details);
readline.close();
});
});
});
Output:
What is your name?
Gfg
What is your email?
gfg@example.com
What is your age?
22
Your entered details: { name: 'Gfg', email: 'gfg@example.com', age: '22' }
- Sequential readline.question() calls are used to ask multiple questions one after another, ensuring inputs are collected in order.
- User responses are stored in the details object (name, email, age) as each answer is received.
- The interface is closed after the final input, and all collected details are printed to the console.