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. zsh: command not found: kid Install the package locally: npm i vue-cli -g Then link it: npm link 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. console.log(process.argv) 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.

npm i commander --save

Add Interactive Prompts

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

npm i inquirer --save

Execute Shell Scripts

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

npm i shelljs --save

Example usage:

cd ..
kid init
# enter project name

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.

npm publish
tnpm publish

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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

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

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.