Building a CLI Tool with Node.js: From Zero to npm
A step-by-step guide to building, testing, and publishing a command-line tool.
Why Build a CLI?
Solves YOUR problem (automate repetitive tasks)
Portfolio piece (shows you can ship complete tools)
Potential income (people pay for useful CLIs)
Fun to build (instant gratification — no UI needed)
What We'll Build
A jsonfmt tool that formats JSON files in place:
$ cat messy.json { "name" : "Alex" , "age" :30, "skills" :[ "js" , "python" ]}
$ jsonfmt messy.json
$ cat messy.json { "name" : "Alex" , "age" : 30, "skills" : [ "js" , "python" ] }
Enter fullscreen mode
Exit fullscreen mode
Step 1: Project Setup
mkdir jsonfmt && cd jsonfmt npm init -y
# Install dependencies npm install commander chalk fs-extra
# Install dev dependencies npm install -D jest typescript @types/node tsup
Enter fullscreen mode
Exit fullscreen mode
// package.json { "name" : "jsonfmt" , "version" : "1.0.0" , "description" : "Format JSON files beautifully" , "type" :…