Skip to content

How to create a cli tool with node.js

Image of the author

David Cojocaru @cojocaru-david

How to Create a CLI Tool with Node.js visual cover image

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:

Prerequisites

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

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

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

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.