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.
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 clonecopies 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.jsand add test code. Run the script with
node. The shebang
#!/usr/bin/env nodetells the terminal to execute the file with Node.
Create a Command
Add a
binfield in
package.jsonto 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
kidcommand 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
kidworks and prints “hello world!”.
View Version Information
Use
process.argvto read command arguments. Add code to output the version from
package.jsonwhen
-vis passed.
<code>console.log(process.argv)</code>Running
kid -v -h -lalalashows the arguments array.
Initialize a Project
Implement
kid initto scaffold a new project: change to target directory, run
kid init, and the CLI clones a template repository via
gitand installs dependencies.
Handle Complex Commands
Use the
commanderpackage to define commands, options, and help output.
<code>npm i commander --save</code>Add Interactive Prompts
Use
inquirerto ask the user for input, such as the project name.
<code>npm i inquirer --save</code>Execute Shell Scripts
Use
shelljsto 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 proxyand
kid tencentto 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.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.