Skip to content
Related Articles

Related Articles

Improve Article

How to Build a JavaScript Command Line Interface (CLI) with Node.js

  • Difficulty Level : Expert
  • Last Updated : 04 Aug, 2021
Geek Week

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :