CLI Applications Introduction
Learn how to create and run Command-Line Interface (CLI) applications using Node.js.
1. What Is a CLI Application?
CLI stands for Command-Line Interface. A CLI application is a program that users interact with by typing commands or entering information in a terminal instead of clicking buttons in a graphical user interface.
Node.js allows developers to create CLI applications using JavaScript. These applications can perform tasks such as reading files, managing data, processing commands, and automating repetitive work.
2. Examples of CLI Applications
| Application | Purpose |
|---|---|
| Task Manager | Add, view, update, and delete tasks. |
| File Manager | Create, read, rename, and delete files. |
| Calculator | Perform mathematical calculations. |
| Student Record System | Manage student information through commands. |
| Automation Tool | Automate repetitive development tasks. |
3. How Does a CLI Application Work?
A CLI application follows a simple process: the user provides input, the application processes that input, and the result is displayed in the terminal.
User
Enters a command or information
Terminal
Receives the user's input
Node.js Application
Processes the input using JavaScript
Output
Displays the result in the terminal
4. Requirements for Creating a CLI Application
Before creating a Node.js CLI application, make sure you have:
- Node.js installed on your computer.
- A code editor such as Visual Studio Code.
- A terminal such as Command Prompt or PowerShell.
- Basic knowledge of JavaScript.
To check whether Node.js and npm are installed, run:
node --version
npm --version
Example output:
v22.x.x
10.x.x
The displayed version numbers may differ depending on your installation.
5. Create Your First CLI Application
Let's create a simple CLI application that displays a welcome message in the terminal.
Step 1: Create a Project Folder
Open your terminal and run:
mkdir MyFirstCLI
cd MyFirstCLI
Step 2: Create a JavaScript File
Create a file named app.js inside the project folder.
Step 3: Write the Code
console.log("Welcome to My First CLI Application!");
console.log("Node.js makes CLI development easy.");
Step 4: Run the Application
Execute the following command from the project folder:
node app.js
Expected Output:
Welcome to My First CLI Application!
Node.js makes CLI development easy.
console.log().
6. CLI vs GUI
| CLI | GUI |
|---|---|
| Command-Line Interface | Graphical User Interface |
| Uses text commands and terminal input. | Uses windows, buttons, menus, and icons. |
| Often useful for automation and developer tools. | Often useful for applications with visual controls. |
Example: Running node app.js. |
Example: Clicking a button in a desktop application. |
7. Important Commands
| Command | Purpose |
|---|---|
node --version |
Displays the installed Node.js version. |
npm --version |
Displays the installed npm version. |
node app.js |
Runs the app.js JavaScript file. |
mkdir MyFirstCLI |
Creates a project directory. |
cd MyFirstCLI |
Moves into the project directory. |
8. Practice Task
Task: Create a Student Welcome CLI
Create a file named student.js and display:
Welcome to CIIT Training Institute!
Student Name: Rahul
Course: Node.js
Learning Mode: Practical
Hint: Use four
console.log() statements and run the file
using node student.js.
Summary
A CLI application is a program that users interact with through
a terminal by entering commands or information. Node.js allows
developers to create CLI applications using JavaScript.
We created our first CLI application using
console.log() and executed it with the
node app.js command.
CLI applications are useful for task management, file operations, automation, and developer tools. At CIIT Training Institute, these concepts help learners build practical Node.js applications.