The Wayback Machine - https://web.archive.org/web/20240725192918/https://www.geeksforgeeks.org/how-to-build-your-own-cli-command-line-interface-with-node-js/
Open In App

How to build your own CLI (Command Line Interface) with Node.js ?

Last Updated : 25 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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 user input with ease. In this article, we will be building a simple CLI tool with the help of Node.js.

Prerequisites:

Steps to create the CLI:

Step 1: Create a folder for the project and open your favorite IDE (eg- VS Code, Atom, etc) within that folder.

Step 2: Open a terminal within the project folder and type npm init -y which will simply generate an empty npm project without going through an interactive process.

Step 3: Let’s install some open-sourced npm packages required for this project:

npm install figlet
npm install inquirer
npm install gradient-string

To know more about these packages, check out their official documentation.

Step 4: Create a file named index.js inside the project folder.

Step 5: Now, let’s write the following code inside the index.js file:

Approach: We will create only a single function to greet the user. Within the greet() function there will be three sections. First, we will display the “Geeks CLI” welcome message on the terminal. For this, we will be using the figlet package which is a program that generates texts based on ASCII characters. Secondly, we will prompt the user to enter his/her name in the terminal using the inquirer package and store it in a variable. Lastly, we will greet the user with his/her name and also color the message using the gradients-string package.

Javascript




import figlet from "figlet";
import inquirer from "inquirer";
import gradient from "gradient-string";
  
// Declare a variable to store the user's name
let userName;
  
const greet = async () => {
  
    // Displaying Geeks CLI
    figlet('Geeks CLI', function (err, data) {
        console.log(data)
    });
  
    // Wait for 2secs
    await new Promise(resolve => setTimeout(resolve, 2000));
  
    // Ask the user's name
    const { name } = await inquirer.prompt({
        type: "input",
        name: "name",
        message: "Enter your name?"
    });
  
    // Set the user's name
    userName = name;
  
    // Print the welcome message
    const msg = `Hello ${userName}!`;
    figlet(msg, (err, data) => {
        console.log(gradient.pastel.multiline(data));
    });
}
  
// Call the askName function
greet();


Step 6: Let’s run the application. Type node index.js in the terminal.

Output:

Geeks CLI Demo



Similar Reads

How to Build a JavaScript Command Line Interface (CLI) with Node.js
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 popula
4 min read
How To Build Your Very Own WordPress Plugin?
In WordPress, a plugin is a small piece of software that adds specific features or functionalities to a WordPress website. Plugins allow users to extend and enhance the capabilities of their WordPress site without having to write code from scratch. They can be used to add a wide variety of features, such as contact forms, SEO tools, security enhanc
4 min read
How to add records in your own local/custom database in Node.js ?
The custom database signifies the local database in your file system. There are two types of database 'SQL' and 'NoSQL'. In SQL database data are stored as table manner and in Nosql database data are stored independently with some particular way to identify each record independently. We can also create our own database or datastore locally in Nosql
3 min read
Creating Your Own Node HTTP Request Router
In Node, if we want to build our own custom HTTP request router then we can create it by handling the incoming requests and directing them to the specific endpoints. Using this routing process, allows us to define the different actions or responses that are based on the requested URLs. In this article, we will create our own node HTTP request route
3 min read
Which command is used to run SASS code from the command line ?
In this article, we will discuss how to run SASS code from the command line. SASS (Syntactically Awesome Style Sheet) is a simple CSS pre-processor that makes it possible to create manageable, platform-independent, and reusable style sheets for websites. It is fully compatible with every version of CSS. Sass reduces the repetition of CSS and theref
3 min read
Build an App with Angular and Angular CLI
Angular is a powerful front-end framework that allows developers to create robust web applications. Angular is based on the Model-View-Controller (MVC) architecture and allows for the creation of reusable components, efficient state management, and two-way data binding. The Angular CLI (Command Line Interface) simplifies project setup and developme
3 min read
How To Add Line Break To 'git commit -m' From The Command Line?
In Git, you should frequently include multi-line, detailed messages when committing changes. Adding line breaks can be challenging, even though the git commit -m command is useful for single-line messages. You can use the command line to include line breaks in your commit messages using a variety of techniques that are covered. In this article we h
2 min read
How to make your own countUp.js plugin using JavaScript ?
prerequisite: Document Object Model ( DOM ) Use of Classes in ES6 If you are a beginner in JavaScript and have basic programming knowledge then don't worry, you have landed in a perfect place. Let us divide the problem into multiple segments: Implementing a simple structure using HTML.Providing a basic display style using CSS.The main programming t
2 min read
Step by Step guide to Write your own WordPress Template
Writing your own WordPress template from scratch is fairly simple. If you are into Web Development industry, you might've already heard what "WordPress" is. Maybe a client has mentioned, but you're not familiar with it. Maybe you've already worked with it before, but don't know how to make a theme from scratch. Or maybe you're a complete newbie. Wh
10 min read
Javascript Program To Write Your Own atoi()
The atoi() function in C takes a string (which represents an integer) as an argument and returns its value of type int. So basically the function is used to convert a string argument to an integer. Syntax: int atoi(const char strn) Parameters: The function accepts one parameter strn which refers to the string argument that is needed to be converted
5 min read
three90RightbarBannerImg