How to create a cli tool with node.js

April 11, 2025
5 min read
By Cojocaru David & ChatGPT

Table of Contents

This is a list of all the sections in this post. Click on any of them to jump to that section.

index

Build Your Own Command-Line Tool with Node.js: A Practical Guide

Command-line tools are essential for automating tasks, streamlining workflows, and boosting developer productivity. If you’re looking to create your own CLI tool, Node.js offers a powerful and accessible platform. This guide will walk you through the process of building a CLI tool with Node.js from scratch, covering argument parsing, user input, and publishing your tool for others to use.

Why Choose Node.js for CLI Development?

Node.js has become a popular choice for CLI development due to its robust ecosystem, cross-platform compatibility, and ease of use. Here’s why it’s a great option:

  • Leverage Your JavaScript Skills: If you’re already familiar with JavaScript, you can apply your existing knowledge without learning a new language.
  • Extensive NPM Packages: Access a vast library of modules through NPM, including popular choices like commander and yargs for simplifying argument parsing.
  • Cross-Platform Compatibility: Your CLI tool can run seamlessly on Windows, macOS, and Linux with minimal modifications.
  • Efficient Execution: Node.js is known for its speed and efficiency, making it ideal for building lightweight command-line utilities.

Prerequisites

Before we begin, make sure you have the following installed:

  • Node.js (version 16 or later recommended)
  • NPM or Yarn (for package management)
  • A terminal or command prompt

Project Setup

Let’s start by creating a new directory for your CLI tool and initializing a Node.js project:

mkdir my-cli-tool
cd my-cli-tool
npm init -y

Next, install the necessary dependencies. We’ll use commander for argument parsing and chalk for adding color to the output:

npm install commander chalk
  • commander: Simplifies the process of defining commands, options, and parsing user input.
  • chalk: Enhances the user experience by adding color and styling to terminal output.

Creating the CLI Entry Point

Now, create an index.js file in your project’s root directory and add the following code:

#!/usr/bin/env node
const { program } = require("commander");
const chalk = require("chalk");
 
program
  .version("1.0.0")
  .description("A simple CLI tool built with Node.js")
  .option("-n, --name <name>", "Specify a name to greet")
  .parse(process.argv);
 
const options = program.opts();
 
if (options.name) {
  console.log(chalk.green(`Hello, ${options.name}!`));
} else {
  console.log(chalk.yellow("Please provide a name using the --name option."));
}

Making the Script Executable

To make your script executable, you need to update the package.json file. Add a bin field that maps the command name to your script:

"bin": {
  "my-cli": "./index.js"
}

This tells NPM to create a symbolic link from index.js to /usr/local/bin/my-cli (or a similar location depending on your system).

After modifying package.json, run the following command to link your CLI tool for local testing:

npm link

Now you can test your tool by running:

my-cli --name Alice

Handling User Input and Commands

A robust CLI tool should support multiple commands and options. Here’s how to add a new command to your tool:

program
  .command("calculate <num1> <num2>")
  .description("Adds two numbers together")
  .action((num1, num2) => {
    const sum = parseInt(num1, 10) + parseInt(num2, 10);
    console.log(chalk.blue(`The sum is: ${sum}`));
  });

With this addition, users can run:

my-cli calculate 5 10

This will output: “The sum is: 15” in blue.

Publishing Your CLI Tool to NPM

Once your CLI tool is ready for the world, you can publish it to NPM:

  1. Create an NPM Account: If you don’t already have one, sign up for an account at npmjs.com.
  2. Log In: Open your terminal and run npm login. Enter your username, password, and email address.
  3. Publish: Navigate to your project directory in the terminal and run npm publish.

After publishing, anyone can install your tool globally using:

npm install -g my-cli-tool

Remember to choose a unique package name to avoid conflicts on NPM.

Best Practices for CLI Development

  • Comprehensive Documentation: Include a --help flag to display usage instructions and create a detailed README file.
  • Robust Error Handling: Validate user inputs and provide informative error messages to guide users.
  • Thorough Testing: Implement unit tests using frameworks like jest or mocha to ensure reliability and prevent regressions.
  • Optimize Performance: Strive for speed and efficiency, especially for frequently used commands.
  • Follow Conventions: Adhere to common CLI conventions for argument parsing and command structure.

Conclusion

Building a CLI tool with Node.js is a rewarding experience that can significantly enhance your productivity and automation capabilities. By leveraging the power of Node.js and libraries like commander and chalk, you can create user-friendly and efficient tools for a wide range of tasks. Start small, iterate, and enjoy the process of bringing your ideas to life on the command line.