How to build your own CLI (Command Line Interface) with Node.js ?
Last Updated :
25 Mar, 2022
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 to
4 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
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 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 install modules without npm in node.js ? We can install modules required for a particular project in node.js without npm, the recommended node package manager using yarn. Yarn is a wonderful package manager. Like npm, if you have a project folder with package.json containing all the required dependencies mentioned for the project, you can
3 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