JavaScript

Building CLI Tools

Create your own command-line tools! Learn to build scripts that run in the terminal using Node.js.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Create your own command-line tools! Learn to build scripts that run in the terminal using Node.js. This hands-on tutorial focuses on practical implementation of building cli tools concepts.

Building CLI Tools

You've used CLI (Command Line Interface) tools like git, npm, and node. Now, let's build our own!

A CLI tool is just a Node.js script that you run from the terminal to perform tasks (like creating files, calculating math, or fetching data).

1. The Shebang Line (#!) πŸ’₯

To make a script executable like a real program (instead of typing node script.js), we add a special line at the top.

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

This tells the operating system: "Use Node.js to run this file."

2. Reading Arguments (process.argv) πŸ“₯

When you run node app.js hello world, how does your code know you typed "hello" and "world"?

It uses process.argv (Argument Vector). It's an array containing:

  1. Path to Node.js executable.
  2. Path to your script file.
  3. Your arguments (starting from index 2).
JAVASCRIPT PLAYGROUND
⏳ Loading editor…

3. Interactive Input (readline) πŸ’¬

Sometimes you want to ask the user a question.

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

4. Practical Example: File Creator πŸ› οΈ

Let's build a tool that creates a file with some text. Usage: node create.js <filename> <text>

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

5. Making it Global 🌍

To run your tool from any folder (just by typing mytool), you need to:

  1. Add "bin" to your package.json.
  2. Run npm link.
// package.json
{
  "name": "my-cool-cli",
  "bin": {
    "mytool": "./index.js"
  }
}

Now, typing mytool in the terminal runs your script!

AI Mentor

Confused about "Node.js CLI tools, process.argv, readline, and creating global commands"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 4

What does the shebang line (#!/usr/bin/env node) do?

Imports Node.js modules
Tells the OS to execute the file using Node.js
Comments out the code
Sets the environment variables

Key Takeaways

βœ… process.argv reads command-line inputs.
βœ… process.exit(1) quits with an error.
βœ… readline takes interactive user input.
βœ… bin in package.json creates global commands.

What's Next?

We've mastered the terminal. Now let's conquer the web! Next up: HTTP Server Basics.

Keep coding! πŸš€