Backend Development 10 min read

How to Build Your Own Command-Line Tool with Node.js

This guide explains what a command-line interface (CLI) tool is, why developers prefer CLI over GUI for efficiency, and provides a step‑by‑step tutorial for creating a simple Node.js‑based CLI, covering project setup, command registration, argument parsing, version handling, interactive prompts, shell script execution, and network proxy management.

Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
How to Build Your Own Command-Line Tool with Node.js

What is a Command-Line Tool?

Command-line interface (CLI) tools are used in the terminal. Common examples include

git

,

npm

,

vim

. For example,

git clone

copies remote code locally.

Why Use CLI Tools?

Compared with graphical user interface (GUI), CLI offers higher efficiency, automation, and composability. GUI is convenient for simple tasks but limits access to full tool capabilities. CLI enables custom commands, scripting, and macro creation.

How to Develop a CLI Tool?

Any language can be used; this tutorial uses Node.js for front‑end developers.

Create a Project

Initialize a Node.js project.

Install the code command, open VS Code, and run the command palette (⇧⌘P) to select “Install 'code' command in PATH”.

Open

index.js

and add test code. Run the script with

node

. The shebang

#!/usr/bin/env node

tells the terminal to execute the file with Node.

Create a Command

Add a

bin

field in

package.json

to map a command name (e.g.,

kid

) to the executable file.

If multiple commands are needed, add them to the bin object.

After linking the package locally with

npm link

, the

kid

command becomes available.

<code>zsh: command not found: kid</code>

Install the package locally:

<code>npm i vue-cli -g</code>

Then link it:

<code>npm link</code>

Now

kid

works and prints “hello world!”.

View Version Information

Use

process.argv

to read command arguments. Add code to output the version from

package.json

when

-v

is passed.

<code>console.log(process.argv)</code>

Running

kid -v -h -lalala

shows the arguments array.

Initialize a Project

Implement

kid init

to scaffold a new project: change to target directory, run

kid init

, and the CLI clones a template repository via

git

and installs dependencies.

Handle Complex Commands

Use the

commander

package to define commands, options, and help output.

<code>npm i commander --save</code>

Add Interactive Prompts

Use

inquirer

to ask the user for input, such as the project name.

<code>npm i inquirer --save</code>

Execute Shell Scripts

Use

shelljs

to run shell commands like cloning a repository and installing dependencies.

<code>npm i shelljs --save</code>

Example usage:

<code>cd ..
kid init
# enter project name</code>

Switch Network Proxy

Provide commands

kid proxy

and

kid tencent

to toggle npm proxy settings using

shelljs

.

Conclusion

Publish the CLI tool to npm or tnpm for others to use.

<code>npm publish
tnpm publish</code>

The tutorial demonstrates that building a CLI tool is approachable and encourages developers to create their own utilities.

CLINode.jscommanderinquirernpmcommand-line toolshelljs
Tencent IMWeb Frontend Team
Written by

Tencent IMWeb Frontend Team

IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.