Why do you need CLI utilities in JavaScript?
Imagine: you manually rename report folders, convert JSON to CSV, or search for duplicate files every day. What if you could write a small program that does this in a second with a terminal command? That's exactly what CLI utilities in JavaScript are for.
Unlike full-fledged web applications, CLI (Command Line Interface) tools are lightweight, fast, and very convenient. They don't require a browser, work on any platform, and easily integrate into CI/CD scripts. At PythonLib, we often discuss automation, and today I'll show you how to create your first CLI utility in Node.js from scratch. You'll be surprised how simple it is!
In this article, we'll go through the entire process: from project setup to publishing a ready-made package to npm. You'll get not just theory, but working code that you can immediately adapt to your tasks.
What is a CLI utility and why JavaScript?
A CLI utility is a program that runs from the command line. You type my-tool --input file.txt — and it performs the required action. JavaScript is ideal for this because:
- Node.js is installed on every server and almost every developer's PC.
- A huge number of ready-made modules (npm) for working with files, networks, and data formats.
- Simplicity of writing: no compilation needed, just one file is enough.
- Cross-platform: your tool will work on Windows, macOS, and Linux.
Even if you are a beginner in Node.js, you can handle it. We'll start with the simplest thing — a script that prints a greeting.
Step 1: Setting up the project for a CLI utility
Create a folder for your project and initialize npm:
mkdir my-first-clicd my-first-clinpm init -yNow open package.json. This is the main config for your future CLI utility in JavaScript. Add the "bin" field to it:
{ "name": "my-first-cli", "version": "1.0.0", "description": "My first CLI utility", "bin": { "greet": "./index.js" }, "license": "MIT"}The "bin" field is the magic. It tells Node.js: "When the user enters the command greet, run the file index.js". Note: the command name (greet) can be anything.
Now create index.js with the first line — a shebang. This is mandatory for CLI:
#!/usr/bin/env node
console.log('Hello, world! I am your first CLI utility!');The shebang #!/usr/bin/env node tells the system that the script should be executed using Node.js. Without it, the command won't work.
Step 2: Making the utility executable and testing it
To run your CLI utility in JavaScript locally, execute the command:
npm linkThis command creates a global link to your package. Now in any terminal you can type:
greetAnd you will see: Hello, world! I am your first CLI utility! Congratulations, you have just created your first utility!
Step 3: Parsing command line arguments
Just printing text is boring. Real CLI utilities accept arguments. Let's learn how to handle them. Node.js has a built-in array process.argv, but for serious projects it's better to use the commander or yargs module.
Let's install commander:
npm install commanderNow rewrite index.js:
#!/usr/bin/env node
const { Command } = require('commander');const program = new Command();
program .name