How to Build a JavaScript Command Line Interface (CLI) with Node.js
Last Updated :
30 Jul, 2024
In this article, we will see how to create a JavaScript CLI with the help of NodeJs.
JavaScript being an interpreter language, it is a very dynamic language, due to which it provides us with many facilities, due to which many tasks become very easy and anyone can understand them very easily, so in today's era JavaScript It is one of the most popular programming languages of the world, that is why today many tools are being built in it.
In this article, we will build a CLI application with the help of JavaScript. We need NodeJs to run JavaScript directly. If you have not installed NodeJs on your computer or facing any problem in NodeJs, then refer to this article.
Approach: We want to make a CLI application in JavaScript but JavaScript runs only on the browser but if we want to use it outside the browser then we will need a run-time that NodeJS provides so we have to use NodeJs. Then we will be able to create a CLI application with the help of JavaScript. We will use readline module of nodejs.
Step by step implementation:
Step 1: In the first step, we set up a NodeJs project environment. If you do have not installed NPM or NodeJs please refer to this article.
- Run the below command and initiate the NodeJs project.
npm init -y
Use "-y" to keep default answers to all questions.
Project Structure: After doing the above things move to create an index.js file for writing our code, and the directory looks like this.

.Step 2: Now we will see the working of the NodeJs readline module.
The readline module allows reading a data stream one line at a time. It can be included in the code with the require() method. We can enter any stream of data we want in the readline module, which allows us to work with standard input and output streams. Or the readline module provides an interface for reading data from a Readable stream (such as process.stdin) one line at a time. It can be accessed using. The question method of the readline provides a facility to work a query-response mechanism we use the question method and pass two arguments first one is a query and the second one is a custom handler to take an argument as an input of the handler function.
Syntax:
readline.question(query, callback);
Accepts the display query string and the callback function to be invoked as a response of the user's input to the query. The createInterface method of the readline provides an interface object to work with readline and their methods.
Syntax:
readline.createInterface();
Now let's see the code implementation of the above approach. The below code shows the use of readline module for accepting input from the user as a CLI.
index.js
// import the readline module for work with stdin, or stdout.
const readline = require('readline');
// create a readline object to work with the stream.
// pass the stdin, or stdout in the current process.
const prompts = readline.createInterface(process.stdin, process.stdout);
// create a question or there handler.
prompts.question('Enter Learning Resources Name : ', (response) => {
// check the response.
if(response.toLocaleLowerCase() == 'gfg') {
console.log("You are a part of the very huge learning community.");
}else {
console.log("Have a look at Geeksforgeeks, they solve many of your
technical doubts.");
}
// after the all work is done want to terminate this process.
process.exit();
});
Step to run the application:
node index.js
Output:
Explanation: The createInterface method is used to create an interface to take user input. It takes two parameters, the standard input (stdin) and standard output (stdout) of the current process. Next, the question() method of the createInterface object is used to prompt a question to the user. The second parameter is a callback function the will process the user's input passed to it as a parameter. The logic inside the function will display an appropriate message based on the evaluation of the user's input value. The process.exit() statement allows to exit the code else the interface will keep reading from the standard input.
Similar Reads
How to build your own CLI (Command Line Interface) with Node.js ?
Introduction: A command-line interface (CLI) is a text-based user interface (UI) for running programs, managing files, and interacting with computers. Building your own CLI is easier than you might think with Node.js. There are a bunch of open-source packages that can handle color, animation, and us
2 min read
How to write âBeautiful Lifeâ in Node.js ?
Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework and itâs not a programming language. Most people are confused and understand itâs a framework or a programming language. We often use
1 min read
Difference between Node.js and JavaScript
JavaScript and Node.js are both crucial in modern web development, but they serve different purposes and are used in different environments. JavaScript is a programming language primarily used for client-side web development, while Node is a runtime environment that allows JavaScript to be executed
3 min read
How to Read Command Line Arguments in Node ?
Command-line arguments (CLI) consist of text strings utilized to provide extra information to a program during its execution via the command line interface of an operating system. Node facilitates the retrieval of these arguments through the global object, specifically the process object. ApproachTo
2 min read
How to Parse Command Line Arguments in Node ?
Command-line arguments, in the context of a command-line interface (CLI), are text strings that provide extra information to a program when it is executed.In Nodejs, these arguments are accessible through an array known as argv (arguments values), where the shell passes all received command-line arg
3 min read
How to Configure Socket.IO with Demo-Chat App in Node.js ?
For making a chat app, it is required that the server sends data to the client but the client should also respond back to the server. This can be done by making use of the Websockets. A WebSocket is a kind of communication pipe opened in two directions. Prerequisite: Node.js: It is an open source Ja
5 min read
Command Line Flags by the Node Binary
In this article, we will learn about the command line flags offered by the Node binary. The Node.js platform is almost entirely represented by the node binary executable. In order to execute a JavaScript program we use: node app.js, where app.js is the program we wish to run. However, before we can
6 min read
How to print command line arguments passed to the script in Node.js ?
Node.js is an open-source and cross-platform runtime environment built on Chrome's V8 engine that enables us to use JavaScript outside the browser. Node.js helps us to use build server-side applications using JavaScript. In Node.js if you want to print the command line arguments then we can access
2 min read
How much JavaScript do you need to know to use Node.js?
NodeJS has revolutionized web development by enabling developers to create powerful server-side applications using JavaScript. If you're new to NodeJS, you might wonder how much JavaScript knowledge you need to get started. In this guide, we'll explore the essential JavaScript skills required to div
8 min read
How to Build a Simple Web Server with Node.js ?
Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework, and itâs not a programming language. Node.js is mostly used in server-side programming. In this article, we will discuss how to make
3 min read